2015-12-11 01:52:36 +00:00
|
|
|
"""
|
2016-02-23 05:21:49 +00:00
|
|
|
Support for the Twitch stream status.
|
2015-12-11 01:52:36 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-12-16 01:43:14 +00:00
|
|
|
https://home-assistant.io/components/sensor.twitch/
|
2015-12-11 01:52:36 +00:00
|
|
|
"""
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-12-11 01:52:36 +00:00
|
|
|
|
2015-12-16 01:43:14 +00:00
|
|
|
STATE_STREAMING = 'streaming'
|
|
|
|
STATE_OFFLINE = 'offline'
|
|
|
|
ATTR_GAME = 'game'
|
|
|
|
ATTR_TITLE = 'title'
|
|
|
|
ICON = 'mdi:twitch'
|
2015-12-11 01:52:36 +00:00
|
|
|
|
2016-07-17 20:05:50 +00:00
|
|
|
REQUIREMENTS = ['python-twitch==1.3.0']
|
2015-12-11 01:52:36 +00:00
|
|
|
DOMAIN = 'twitch'
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Setup the Twitch platform."""
|
2015-12-11 01:52:36 +00:00
|
|
|
add_devices(
|
2015-12-16 01:43:14 +00:00
|
|
|
[TwitchSensor(channel) for channel in config.get('channels', [])])
|
2015-12-11 01:52:36 +00:00
|
|
|
|
|
|
|
|
2015-12-16 01:43:14 +00:00
|
|
|
class TwitchSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of an Twitch channel."""
|
2015-12-11 01:52:36 +00:00
|
|
|
|
|
|
|
# pylint: disable=abstract-method
|
|
|
|
def __init__(self, channel):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-12-11 01:52:36 +00:00
|
|
|
self._channel = channel
|
2015-12-16 01:43:14 +00:00
|
|
|
self._state = STATE_OFFLINE
|
2015-12-11 01:52:36 +00:00
|
|
|
self._preview = None
|
|
|
|
self._game = None
|
|
|
|
self._title = None
|
2015-12-16 01:43:14 +00:00
|
|
|
self.update()
|
2015-12-11 01:52:36 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Device should be polled."""
|
2015-12-11 01:52:36 +00:00
|
|
|
return True
|
|
|
|
|
2015-12-16 01:43:14 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-12-16 01:43:14 +00:00
|
|
|
return self._channel
|
|
|
|
|
2015-12-11 01:52:36 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2015-12-11 01:52:36 +00:00
|
|
|
return self._state
|
|
|
|
|
2016-02-24 06:41:24 +00:00
|
|
|
@property
|
|
|
|
def entity_picture(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return preview of current game."""
|
2016-02-24 06:41:24 +00:00
|
|
|
return self._preview
|
|
|
|
|
2015-12-11 01:52:36 +00:00
|
|
|
# pylint: disable=no-member
|
|
|
|
def update(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Update device state."""
|
2015-12-11 01:52:36 +00:00
|
|
|
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')
|
2015-12-16 01:43:14 +00:00
|
|
|
self._state = STATE_STREAMING
|
2015-12-11 01:52:36 +00:00
|
|
|
else:
|
2016-02-24 06:41:24 +00:00
|
|
|
self._preview = None
|
2015-12-16 01:43:14 +00:00
|
|
|
self._state = STATE_OFFLINE
|
2015-12-11 01:52:36 +00:00
|
|
|
|
|
|
|
@property
|
2016-02-07 06:28:29 +00:00
|
|
|
def device_state_attributes(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state attributes."""
|
2015-12-16 01:43:14 +00:00
|
|
|
if self._state == STATE_STREAMING:
|
|
|
|
return {
|
|
|
|
ATTR_GAME: self._game,
|
|
|
|
ATTR_TITLE: self._title,
|
|
|
|
}
|
2015-12-11 01:52:36 +00:00
|
|
|
|
|
|
|
@property
|
2015-12-16 01:43:14 +00:00
|
|
|
def icon(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Icon to use in the frontend, if any."""
|
2015-12-16 01:43:14 +00:00
|
|
|
return ICON
|