Imports twitch (#28517)

* Move imports

* Add unique_id
This commit is contained in:
Fabian Affolter 2019-11-04 09:56:46 +01:00 committed by GitHub
parent e91bb1ab08
commit fe00f3558e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,11 +1,13 @@
"""Support for the Twitch stream status.""" """Support for the Twitch stream status."""
import logging import logging
from requests.exceptions import HTTPError
from twitch import TwitchClient
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -14,6 +16,7 @@ ATTR_TITLE = "title"
CONF_CHANNELS = "channels" CONF_CHANNELS = "channels"
CONF_CLIENT_ID = "client_id" CONF_CLIENT_ID = "client_id"
ICON = "mdi:twitch" ICON = "mdi:twitch"
STATE_OFFLINE = "offline" STATE_OFFLINE = "offline"
@ -22,18 +25,16 @@ STATE_STREAMING = "streaming"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
vol.Required(CONF_CLIENT_ID): cv.string, vol.Required(CONF_CLIENT_ID): cv.string,
vol.Required(CONF_CHANNELS, default=[]): vol.All(cv.ensure_list, [cv.string]), vol.Required(CONF_CHANNELS): vol.All(cv.ensure_list, [cv.string]),
} }
) )
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Twitch platform.""" """Set up the Twitch platform."""
from twitch import TwitchClient channels = config[CONF_CHANNELS]
from requests.exceptions import HTTPError client_id = config[CONF_CLIENT_ID]
client = TwitchClient(client_id=client_id)
channels = config.get(CONF_CHANNELS, [])
client = TwitchClient(client_id=config.get(CONF_CLIENT_ID))
try: try:
client.ingests.get_server_list() client.ingests.get_server_list()
@ -55,8 +56,7 @@ class TwitchSensor(Entity):
self._user = user self._user = user
self._channel = self._user.name self._channel = self._user.name
self._id = self._user.id self._id = self._user.id
self._state = STATE_OFFLINE self._state = self._preview = self._game = self._title = None
self._preview = self._game = self._title = None
@property @property
def should_poll(self): def should_poll(self):
@ -84,6 +84,11 @@ class TwitchSensor(Entity):
if self._state == STATE_STREAMING: if self._state == STATE_STREAMING:
return {ATTR_GAME: self._game, ATTR_TITLE: self._title} return {ATTR_GAME: self._game, ATTR_TITLE: self._title}
@property
def unique_id(self):
"""Return unique ID for this sensor."""
return self._id
@property @property
def icon(self): def icon(self):
"""Icon to use in the frontend, if any.""" """Icon to use in the frontend, if any."""