Add last_online and level attribute to steam online (#30321)

* Steam Online: Add last_online and Level attributes

* Steam Online: Add last_online and Level attributes

* Fixes from comments on initial pull request

* Push attributes only if they are not None
This commit is contained in:
Mariano Faraco 2020-01-08 17:04:42 -03:00 committed by Paulus Schoutsen
parent d048233776
commit 9e885112ec

View file

@ -1,6 +1,7 @@
"""Sensor for Steam account status."""
from datetime import timedelta
import logging
from time import mktime
import steam
import voluptuous as vol
@ -11,6 +12,7 @@ from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.util.dt import utc_from_timestamp
_LOGGER = logging.getLogger(__name__)
@ -70,7 +72,12 @@ class SteamSensor(Entity):
self._steamod = steamod
self._account = account
self._profile = None
self._game = self._state = self._name = self._avatar = None
self._game = None
self._state = None
self._name = None
self._avatar = None
self._last_online = None
self._level = None
@property
def name(self):
@ -107,11 +114,19 @@ class SteamSensor(Entity):
}.get(self._profile.status, STATE_OFFLINE)
self._name = self._profile.persona
self._avatar = self._profile.avatar_medium
self._last_online = self._get_last_online()
self._level = self._profile.level
except self._steamod.api.HTTPTimeoutError as error:
_LOGGER.warning(error)
self._game = self._state = self._name = self._avatar = None
self._game = None
self._state = None
self._name = None
self._avatar = None
self._last_online = None
self._level = None
def _get_current_game(self):
"""Gather current game name from APP ID."""
game_id = self._profile.current_game[0]
game_extra_info = self._profile.current_game[2]
@ -140,10 +155,26 @@ class SteamSensor(Entity):
_LOGGER.error("Unable to find name of app with ID=%s", game_id)
return repr(game_id)
def _get_last_online(self):
"""Convert last_online from the steam module into timestamp UTC."""
last_online = utc_from_timestamp(mktime(self._profile.last_online))
if last_online:
return last_online
return None
@property
def device_state_attributes(self):
"""Return the state attributes."""
return {"game": self._game} if self._game else None
attr = {}
if self._game is not None:
attr["game"] = self._game
if self._last_online is not None:
attr["last_online"] = self._last_online
if self._level is not None:
attr["level"] = self._level
return attr
@property
def entity_picture(self):