core/homeassistant/components/sensor/steam_online.py

112 lines
3.0 KiB
Python
Raw Normal View History

2016-02-20 23:08:18 +00:00
"""
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/
"""
2017-03-02 14:58:35 +00:00
import logging
2016-09-04 02:21:59 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
2016-02-20 23:08:18 +00:00
from homeassistant.helpers.entity import Entity
2016-02-24 06:41:24 +00:00
from homeassistant.const import CONF_API_KEY
2016-09-04 02:21:59 +00:00
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['steamodd==4.21']
2017-03-02 14:58:35 +00:00
_LOGGER = logging.getLogger(__name__)
2016-09-04 02:21:59 +00:00
CONF_ACCOUNTS = 'accounts'
2016-02-20 23:08:18 +00:00
ICON = 'mdi:steam'
2016-09-04 02:21:59 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_ACCOUNTS, default=[]):
vol.All(cv.ensure_list, [cv.string]),
})
2016-02-20 23:08:18 +00:00
2017-03-10 03:55:18 +00:00
STATE_ONLINE = 'Online'
STATE_BUSY = 'Busy'
STATE_AWAY = 'Away'
STATE_SNOOZE = 'Snooze'
STATE_TRADE = 'Trade'
STATE_PLAY = 'Play'
2016-02-20 23:08:18 +00:00
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Steam platform."""
2016-02-20 23:08:18 +00:00
import steam as steamod
steamod.api.key.set(config.get(CONF_API_KEY))
add_devices(
[SteamSensor(account,
2016-09-04 02:21:59 +00:00
steamod) for account in config.get(CONF_ACCOUNTS)])
2016-02-20 23:08:18 +00:00
class SteamSensor(Entity):
2016-02-24 09:47:35 +00:00
"""A class for the Steam account."""
2016-03-08 15:46:34 +00:00
2016-02-20 23:08:18 +00:00
def __init__(self, account, steamod):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2016-02-20 23:08:18 +00:00
self._steamod = steamod
self._account = account
self.update()
@property
def name(self):
2016-02-24 09:47:35 +00:00
"""Return the name of the sensor."""
2017-03-02 14:58:35 +00:00
return self._name
2016-02-20 23:08:18 +00:00
@property
def entity_id(self):
2016-02-24 09:47:35 +00:00
"""Return the entity ID."""
2016-02-20 23:08:18 +00:00
return 'sensor.steam_{}'.format(self._account)
@property
def state(self):
2016-02-24 09:47:35 +00:00
"""Return the state of the sensor."""
2016-02-23 18:01:50 +00:00
return self._state
2016-02-20 23:08:18 +00:00
# pylint: disable=no-member
def update(self):
2016-02-24 09:47:35 +00:00
"""Update device state."""
2017-03-02 14:58:35 +00:00
try:
self._profile = self._steamod.user.profile(self._account)
if self._profile.current_game[2] is None:
self._game = 'None'
else:
self._game = self._profile.current_game[2]
self._state = {
2017-03-10 03:55:18 +00:00
1: STATE_ONLINE,
2: STATE_BUSY,
3: STATE_AWAY,
4: STATE_SNOOZE,
5: STATE_TRADE,
6: STATE_PLAY,
2017-03-02 14:58:35 +00:00
}.get(self._profile.status, 'Offline')
self._name = self._profile.persona
self._avatar = self._profile.avatar_medium
except self._steamod.api.HTTPTimeoutError as error:
_LOGGER.warning(error)
self._game = 'Unknown'
self._state = 'Unknown'
self._name = 'Unknown'
self._avatar = None
2016-02-20 23:08:18 +00:00
@property
def device_state_attributes(self):
2016-03-08 15:46:34 +00:00
"""Return the state attributes."""
return {'Game': self._game}
2016-02-20 23:08:18 +00:00
@property
2016-02-24 06:41:24 +00:00
def entity_picture(self):
"""Avatar of the account."""
2017-03-02 14:58:35 +00:00
return self._avatar
2016-02-20 23:08:18 +00:00
@property
def icon(self):
2016-02-24 09:47:35 +00:00
"""Return the icon to use in the frontend."""
2016-02-20 23:08:18 +00:00
return ICON