From 532d807771a302800594cb742f1c7eb14415f141 Mon Sep 17 00:00:00 2001 From: Rowan Hine Date: Sat, 20 Feb 2016 23:08:18 +0000 Subject: [PATCH 1/2] Add Steam sensor --- .coveragerc | 1 + .../components/sensor/steam_online.py | 80 +++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 84 insertions(+) create mode 100644 homeassistant/components/sensor/steam_online.py diff --git a/.coveragerc b/.coveragerc index 27016770693..1e7f0b0a88d 100644 --- a/.coveragerc +++ b/.coveragerc @@ -130,6 +130,7 @@ omit = homeassistant/components/sensor/openweathermap.py homeassistant/components/sensor/rest.py homeassistant/components/sensor/sabnzbd.py + homeassistant/components/sensor/steam_online.py homeassistant/components/sensor/speedtest.py homeassistant/components/sensor/swiss_public_transport.py homeassistant/components/sensor/systemmonitor.py diff --git a/homeassistant/components/sensor/steam_online.py b/homeassistant/components/sensor/steam_online.py new file mode 100644 index 00000000000..1264e9a8556 --- /dev/null +++ b/homeassistant/components/sensor/steam_online.py @@ -0,0 +1,80 @@ +""" +homeassistant.components.sensor.steam_online +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Sensor for Steam account status. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.steam_online/ +""" +from homeassistant.helpers.entity import Entity +from homeassistant.const import (ATTR_ENTITY_PICTURE, CONF_API_KEY) + +ICON = 'mdi:steam' + +REQUIREMENTS = ['steamodd==4.21'] + + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Sets up the Steam platform. """ + import steam as steamod + steamod.api.key.set(config.get(CONF_API_KEY)) + add_devices( + [SteamSensor(account, + steamod) for account in config.get('accounts', [])]) + + +class SteamSensor(Entity): + """ Steam account. """ + + # pylint: disable=abstract-method + def __init__(self, account, steamod): + self._steamod = steamod + self._account = account + self.update() + + @property + def name(self): + """ Returns the name of the sensor. """ + return self._profile.persona + + @property + def entity_id(self): + """ Entity ID. """ + return 'sensor.steam_{}'.format(self._account) + + @property + def state(self): + """ State of the sensor. """ + if self._profile.status == 1: + account_state = 'Online' + elif self._profile.status == 2: + account_state = 'Busy' + elif self._profile.status == 3: + account_state = 'Away' + elif self._profile.status == 4: + account_state = 'Snooze' + elif self._profile.status == 5: + account_state = 'Trade' + elif self._profile.status == 6: + account_state = 'Play' + else: + account_state = 'Offline' + return account_state + + # pylint: disable=no-member + def update(self): + """ Update device state. """ + self._profile = self._steamod.user.profile(self._account) + + @property + def device_state_attributes(self): + """ Returns the state attributes. """ + return { + ATTR_ENTITY_PICTURE: self._profile.avatar_medium + } + + @property + def icon(self): + """ Icon to use in the frontend """ + return ICON diff --git a/requirements_all.txt b/requirements_all.txt index 1daebdb447f..0bcfae358f2 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -240,6 +240,9 @@ somecomfort==0.2.0 # homeassistant.components.sensor.speedtest speedtest-cli==0.3.4 +# homeassistant.components.sensor.steam_online +steamodd==4.21 + # homeassistant.components.light.tellstick # homeassistant.components.sensor.tellstick # homeassistant.components.switch.tellstick From f8240a9cda5717de696899f4f643ee70ae5ea743 Mon Sep 17 00:00:00 2001 From: Rowan Hine Date: Tue, 23 Feb 2016 18:01:50 +0000 Subject: [PATCH 2/2] Changed to use dictionary --- .../components/sensor/steam_online.py | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/sensor/steam_online.py b/homeassistant/components/sensor/steam_online.py index 1264e9a8556..6b740d0639b 100644 --- a/homeassistant/components/sensor/steam_online.py +++ b/homeassistant/components/sensor/steam_online.py @@ -46,26 +46,20 @@ class SteamSensor(Entity): @property def state(self): """ State of the sensor. """ - if self._profile.status == 1: - account_state = 'Online' - elif self._profile.status == 2: - account_state = 'Busy' - elif self._profile.status == 3: - account_state = 'Away' - elif self._profile.status == 4: - account_state = 'Snooze' - elif self._profile.status == 5: - account_state = 'Trade' - elif self._profile.status == 6: - account_state = 'Play' - else: - account_state = 'Offline' - return account_state + return self._state # pylint: disable=no-member def update(self): """ Update device state. """ self._profile = self._steamod.user.profile(self._account) + self._state = { + 1: 'Online', + 2: 'Busy', + 3: 'Away', + 4: 'Snooze', + 5: 'Trade', + 6: 'Play', + }.get(self._profile.status, 'Offline') @property def device_state_attributes(self):