diff --git a/.coveragerc b/.coveragerc index 94f4352ef89ff850b39f20d6b801f1cd7cee909b..7d482f4db3f543a5acfaded4827c87ba46f0e342 100644 --- a/.coveragerc +++ b/.coveragerc @@ -100,6 +100,7 @@ omit = homeassistant/components/sensor/temper.py homeassistant/components/sensor/time_date.py homeassistant/components/sensor/transmission.py + homeassistant/components/sensor/twitch.py homeassistant/components/sensor/worldclock.py homeassistant/components/switch/arest.py homeassistant/components/switch/command_switch.py diff --git a/homeassistant/components/sensor/twitch.py b/homeassistant/components/sensor/twitch.py new file mode 100644 index 0000000000000000000000000000000000000000..14a3190e07cbe1a28961b9a816c3b4dd0375bdbd --- /dev/null +++ b/homeassistant/components/sensor/twitch.py @@ -0,0 +1,82 @@ +""" +homeassistant.components.sensor.twitch +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Twitch stream status. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.twitch/ +""" + +from homeassistant.helpers.entity import Entity +from homeassistant.const import ATTR_ENTITY_PICTURE + +STATE_STREAMING = 'streaming' +STATE_OFFLINE = 'offline' +ATTR_GAME = 'game' +ATTR_TITLE = 'title' +ICON = 'mdi:twitch' + +REQUIREMENTS = ['python-twitch==1.2.0'] +DOMAIN = 'twitch' + + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Sets up the Twitch platform. """ + add_devices( + [TwitchSensor(channel) for channel in config.get('channels', [])]) + + +class TwitchSensor(Entity): + """ Represents an Twitch channel. """ + + # pylint: disable=abstract-method + def __init__(self, channel): + self._channel = channel + self._state = STATE_OFFLINE + self._preview = None + self._game = None + self._title = None + self.update() + + @property + def should_poll(self): + """ Device should be polled. """ + return True + + @property + def name(self): + """ Returns the name of the device. """ + return self._channel + + @property + def state(self): + """ State of the sensor. """ + return self._state + + # pylint: disable=no-member + def update(self): + """ Update device state. """ + from twitch.api import v3 as twitch + stream = twitch.streams.by_channel(self._channel).get('stream') + if stream: + self._game = stream.get('channel').get('game') + self._title = stream.get('channel').get('status') + self._preview = stream.get('preview').get('small') + self._state = STATE_STREAMING + else: + self._state = STATE_OFFLINE + + @property + def state_attributes(self): + """ Returns the state attributes. """ + if self._state == STATE_STREAMING: + return { + ATTR_GAME: self._game, + ATTR_TITLE: self._title, + ATTR_ENTITY_PICTURE: self._preview + } + + @property + def icon(self): + return ICON diff --git a/requirements_all.txt b/requirements_all.txt index 5c2927d1bb1e2872907fe6659e58a1df7d1a413c..4cc1b9c9fc171f7a13907ca133619e0858f21c8a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -148,6 +148,9 @@ https://github.com/rkabadi/temper-python/archive/3dbdaf2d87b8db9a3cd6e5585fc7045 # homeassistant.components.switch.transmission transmissionrpc==0.11 +# homeassistant.components.sensor.twitch +python-twitch==1.2.0 + # homeassistant.components.sun astral==0.8.1