Improve steam_online generic typing (#84646)

This commit is contained in:
Marc Mueller 2022-12-27 21:53:07 +01:00 committed by GitHub
parent cf598bb5fd
commit 4d69cf1cc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from typing import Union
import steam import steam
from steam.api import _interface_method as INTMethod from steam.api import _interface_method as INTMethod
@ -15,7 +16,9 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import CONF_ACCOUNTS, DOMAIN, LOGGER from .const import CONF_ACCOUNTS, DOMAIN, LOGGER
class SteamDataUpdateCoordinator(DataUpdateCoordinator): class SteamDataUpdateCoordinator(
DataUpdateCoordinator[dict[str, dict[str, Union[str, int]]]]
):
"""Data update coordinator for the Steam integration.""" """Data update coordinator for the Steam integration."""
config_entry: ConfigEntry config_entry: ConfigEntry

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from datetime import datetime from datetime import datetime
from time import localtime, mktime from time import localtime, mktime
from typing import Optional, cast
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -56,17 +57,17 @@ class SteamSensor(SteamEntity, SensorEntity):
"""Return the state of the sensor.""" """Return the state of the sensor."""
if self.entity_description.key in self.coordinator.data: if self.entity_description.key in self.coordinator.data:
player = self.coordinator.data[self.entity_description.key] player = self.coordinator.data[self.entity_description.key]
return STEAM_STATUSES[player["personastate"]] return STEAM_STATUSES[cast(int, player["personastate"])]
return None return None
@property @property
def extra_state_attributes(self) -> dict[str, str | datetime]: def extra_state_attributes(self) -> dict[str, str | int | datetime]:
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
if self.entity_description.key not in self.coordinator.data: if self.entity_description.key not in self.coordinator.data:
return {} return {}
player = self.coordinator.data[self.entity_description.key] player = self.coordinator.data[self.entity_description.key]
attrs: dict[str, str | datetime] = {} attrs: dict[str, str | int | datetime] = {}
if game := player.get("gameextrainfo"): if game := player.get("gameextrainfo"):
attrs["game"] = game attrs["game"] = game
if game_id := player.get("gameid"): if game_id := player.get("gameid"):
@ -76,9 +77,9 @@ class SteamSensor(SteamEntity, SensorEntity):
attrs["game_image_main"] = f"{game_url}{STEAM_MAIN_IMAGE_FILE}" attrs["game_image_main"] = f"{game_url}{STEAM_MAIN_IMAGE_FILE}"
if info := self._get_game_icon(player): if info := self._get_game_icon(player):
attrs["game_icon"] = f"{STEAM_ICON_URL}{game_id}/{info}.jpg" attrs["game_icon"] = f"{STEAM_ICON_URL}{game_id}/{info}.jpg"
self._attr_name = player["personaname"] self._attr_name = str(player["personaname"]) or None
self._attr_entity_picture = player["avatarmedium"] self._attr_entity_picture = str(player["avatarmedium"]) or None
if last_online := player.get("lastlogoff"): if last_online := cast(Optional[int], player.get("lastlogoff")):
attrs["last_online"] = utc_from_timestamp(mktime(localtime(last_online))) attrs["last_online"] = utc_from_timestamp(mktime(localtime(last_online)))
if level := self.coordinator.data[self.entity_description.key]["level"]: if level := self.coordinator.data[self.entity_description.key]["level"]:
attrs["level"] = level attrs["level"] = level