From d3a21bee82bef1c11c926c28298590cba7df82c9 Mon Sep 17 00:00:00 2001 From: happyleaves Date: Thu, 10 Dec 2015 20:52:36 -0500 Subject: [PATCH 1/3] twitch media player --- .../components/media_player/twitch.py | 87 +++++++++++++++++++ requirements_all.txt | 3 + 2 files changed, 90 insertions(+) create mode 100644 homeassistant/components/media_player/twitch.py diff --git a/homeassistant/components/media_player/twitch.py b/homeassistant/components/media_player/twitch.py new file mode 100644 index 00000000000..4b6705c58fd --- /dev/null +++ b/homeassistant/components/media_player/twitch.py @@ -0,0 +1,87 @@ +""" +homeassistant.components.media_player.twitch +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Twitch stream status. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/media_player.twitch/ +""" + +from homeassistant.const import STATE_PLAYING, STATE_OFF + +from homeassistant.components.media_player import ( + MediaPlayerDevice, MEDIA_TYPE_CHANNEL) + +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( + [TwitchDevice(channel) for channel in config.get('channels', [])]) + + +class TwitchDevice(MediaPlayerDevice): + """ Represents an Twitch channel. """ + + # pylint: disable=abstract-method + def __init__(self, channel): + self._channel = channel + self._state = STATE_OFF + self._preview = None + self._game = None + self._title = None + + @property + def should_poll(self): + """ Device should be polled. """ + return True + + @property + def state(self): + """ State of the player. """ + 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_PLAYING + else: + self._state = STATE_OFF + + @property + def name(self): + """ Channel name. """ + return self._channel + + @property + def media_title(self): + """ Channel title. """ + return self._title + + @property + def app_name(self): + """ Game name. """ + return self._game + + @property + def media_image_url(self): + """ Image preview url of the live stream. """ + return self._preview + + @property + def media_content_type(self): + """ Media type (channel). """ + return MEDIA_TYPE_CHANNEL + + def media_pause(self): + """ Must implement because UI can pause. """ + pass diff --git a/requirements_all.txt b/requirements_all.txt index 15bdeb60a3c..b57e27e8fbd 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -76,6 +76,9 @@ plexapi==1.1.0 # homeassistant.components.media_player.sonos SoCo==0.11.1 +# homeassistant.components.media_player.twitch +python-twitch==1.2.0 + # homeassistant.components.modbus https://github.com/bashwork/pymodbus/archive/d7fc4f1cc975631e0a9011390e8017f64b612661.zip#pymodbus==1.2.0 From 0e9c51a479907b37a687038d24dc3325b011befc Mon Sep 17 00:00:00 2001 From: happyleaves Date: Tue, 15 Dec 2015 20:43:14 -0500 Subject: [PATCH 2/3] twitch sensor --- .coveragerc | 1 + .../{media_player => sensor}/twitch.py | 67 +++++++++---------- 2 files changed, 32 insertions(+), 36 deletions(-) rename homeassistant/components/{media_player => sensor}/twitch.py (55%) diff --git a/.coveragerc b/.coveragerc index be4219e3163..1857179764c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -97,6 +97,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/media_player/twitch.py b/homeassistant/components/sensor/twitch.py similarity index 55% rename from homeassistant/components/media_player/twitch.py rename to homeassistant/components/sensor/twitch.py index 4b6705c58fd..14a3190e07c 100644 --- a/homeassistant/components/media_player/twitch.py +++ b/homeassistant/components/sensor/twitch.py @@ -1,16 +1,20 @@ """ -homeassistant.components.media_player.twitch +homeassistant.components.sensor.twitch ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Twitch stream status. For more details about this platform, please refer to the documentation at -https://home-assistant.io/components/media_player.twitch/ +https://home-assistant.io/components/sensor.twitch/ """ -from homeassistant.const import STATE_PLAYING, STATE_OFF +from homeassistant.helpers.entity import Entity +from homeassistant.const import ATTR_ENTITY_PICTURE -from homeassistant.components.media_player import ( - MediaPlayerDevice, MEDIA_TYPE_CHANNEL) +STATE_STREAMING = 'streaming' +STATE_OFFLINE = 'offline' +ATTR_GAME = 'game' +ATTR_TITLE = 'title' +ICON = 'mdi:twitch' REQUIREMENTS = ['python-twitch==1.2.0'] DOMAIN = 'twitch' @@ -20,28 +24,34 @@ DOMAIN = 'twitch' def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the Twitch platform. """ add_devices( - [TwitchDevice(channel) for channel in config.get('channels', [])]) + [TwitchSensor(channel) for channel in config.get('channels', [])]) -class TwitchDevice(MediaPlayerDevice): +class TwitchSensor(Entity): """ Represents an Twitch channel. """ # pylint: disable=abstract-method def __init__(self, channel): self._channel = channel - self._state = STATE_OFF + 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 player. """ + """ State of the sensor. """ return self._state # pylint: disable=no-member @@ -53,35 +63,20 @@ class TwitchDevice(MediaPlayerDevice): self._game = stream.get('channel').get('game') self._title = stream.get('channel').get('status') self._preview = stream.get('preview').get('small') - self._state = STATE_PLAYING + self._state = STATE_STREAMING else: - self._state = STATE_OFF + self._state = STATE_OFFLINE @property - def name(self): - """ Channel name. """ - return self._channel + 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 media_title(self): - """ Channel title. """ - return self._title - - @property - def app_name(self): - """ Game name. """ - return self._game - - @property - def media_image_url(self): - """ Image preview url of the live stream. """ - return self._preview - - @property - def media_content_type(self): - """ Media type (channel). """ - return MEDIA_TYPE_CHANNEL - - def media_pause(self): - """ Must implement because UI can pause. """ - pass + def icon(self): + return ICON From 2a3e0864109b8427b8f9574b46303d607830ca33 Mon Sep 17 00:00:00 2001 From: happyleaves Date: Tue, 15 Dec 2015 20:44:55 -0500 Subject: [PATCH 3/3] fix requirements comment --- requirements_all.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements_all.txt b/requirements_all.txt index b57e27e8fbd..80bebdafa43 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -76,9 +76,6 @@ plexapi==1.1.0 # homeassistant.components.media_player.sonos SoCo==0.11.1 -# homeassistant.components.media_player.twitch -python-twitch==1.2.0 - # homeassistant.components.modbus https://github.com/bashwork/pymodbus/archive/d7fc4f1cc975631e0a9011390e8017f64b612661.zip#pymodbus==1.2.0 @@ -147,6 +144,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