2019-04-03 15:40:03 +00:00
|
|
|
"""Support for the Twitch stream status."""
|
2016-08-19 07:18:45 +00:00
|
|
|
import logging
|
|
|
|
|
2019-11-04 08:56:46 +00:00
|
|
|
from requests.exceptions import HTTPError
|
|
|
|
from twitch import TwitchClient
|
2016-08-19 07:18:45 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-11-04 08:56:46 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-12-11 01:52:36 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_GAME = "game"
|
|
|
|
ATTR_TITLE = "title"
|
2016-08-20 22:40:16 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CHANNELS = "channels"
|
|
|
|
CONF_CLIENT_ID = "client_id"
|
2019-11-04 08:56:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ICON = "mdi:twitch"
|
2015-12-11 01:52:36 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_OFFLINE = "offline"
|
|
|
|
STATE_STREAMING = "streaming"
|
2016-08-19 07:18:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CLIENT_ID): cv.string,
|
2019-11-04 08:56:46 +00:00
|
|
|
vol.Required(CONF_CHANNELS): vol.All(cv.ensure_list, [cv.string]),
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2016-08-19 07:18:45 +00:00
|
|
|
|
2015-12-11 01:52:36 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Twitch platform."""
|
2019-11-04 08:56:46 +00:00
|
|
|
channels = config[CONF_CHANNELS]
|
|
|
|
client_id = config[CONF_CLIENT_ID]
|
|
|
|
client = TwitchClient(client_id=client_id)
|
2018-09-06 12:08:39 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
client.ingests.get_server_list()
|
|
|
|
except HTTPError:
|
|
|
|
_LOGGER.error("Client ID is not valid")
|
2018-09-14 10:08:33 +00:00
|
|
|
return
|
2018-09-06 12:08:39 +00:00
|
|
|
|
|
|
|
users = client.users.translate_usernames_to_ids(channels)
|
2016-08-19 07:18:45 +00:00
|
|
|
|
2018-09-06 12:08:39 +00:00
|
|
|
add_entities([TwitchSensor(user, client) for user in users], True)
|
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
|
|
|
|
2018-09-06 12:08:39 +00:00
|
|
|
def __init__(self, user, client):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2018-09-06 12:08:39 +00:00
|
|
|
self._client = client
|
|
|
|
self._user = user
|
|
|
|
self._channel = self._user.name
|
|
|
|
self._id = self._user.id
|
2019-11-04 08:56:46 +00:00
|
|
|
self._state = self._preview = self._game = self._title = None
|
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
|
|
|
@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:
|
2019-07-31 19:25:30 +00:00
|
|
|
return {ATTR_GAME: self._game, ATTR_TITLE: self._title}
|
2015-12-11 01:52:36 +00:00
|
|
|
|
2019-11-04 08:56:46 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID for this sensor."""
|
|
|
|
return self._id
|
|
|
|
|
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
|
2018-09-06 12:08:39 +00:00
|
|
|
|
|
|
|
# pylint: disable=no-member
|
|
|
|
def update(self):
|
|
|
|
"""Update device state."""
|
|
|
|
stream = self._client.streams.get_stream_by_user(self._id)
|
|
|
|
if stream:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._game = stream.get("channel").get("game")
|
|
|
|
self._title = stream.get("channel").get("status")
|
|
|
|
self._preview = stream.get("preview").get("medium")
|
2018-09-06 12:08:39 +00:00
|
|
|
self._state = STATE_STREAMING
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._preview = self._client.users.get_by_id(self._id).get("logo")
|
2018-09-06 12:08:39 +00:00
|
|
|
self._state = STATE_OFFLINE
|