mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Improve steam_online
generic typing (#84646)
This commit is contained in:
parent
cf598bb5fd
commit
4d69cf1cc3
@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
from typing import Union
|
||||
|
||||
import steam
|
||||
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
|
||||
|
||||
|
||||
class SteamDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
class SteamDataUpdateCoordinator(
|
||||
DataUpdateCoordinator[dict[str, dict[str, Union[str, int]]]]
|
||||
):
|
||||
"""Data update coordinator for the Steam integration."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from time import localtime, mktime
|
||||
from typing import Optional, cast
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
@ -56,17 +57,17 @@ class SteamSensor(SteamEntity, SensorEntity):
|
||||
"""Return the state of the sensor."""
|
||||
if self.entity_description.key in self.coordinator.data:
|
||||
player = self.coordinator.data[self.entity_description.key]
|
||||
return STEAM_STATUSES[player["personastate"]]
|
||||
return STEAM_STATUSES[cast(int, player["personastate"])]
|
||||
return None
|
||||
|
||||
@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."""
|
||||
if self.entity_description.key not in self.coordinator.data:
|
||||
return {}
|
||||
player = self.coordinator.data[self.entity_description.key]
|
||||
|
||||
attrs: dict[str, str | datetime] = {}
|
||||
attrs: dict[str, str | int | datetime] = {}
|
||||
if game := player.get("gameextrainfo"):
|
||||
attrs["game"] = game
|
||||
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}"
|
||||
if info := self._get_game_icon(player):
|
||||
attrs["game_icon"] = f"{STEAM_ICON_URL}{game_id}/{info}.jpg"
|
||||
self._attr_name = player["personaname"]
|
||||
self._attr_entity_picture = player["avatarmedium"]
|
||||
if last_online := player.get("lastlogoff"):
|
||||
self._attr_name = str(player["personaname"]) or None
|
||||
self._attr_entity_picture = str(player["avatarmedium"]) or None
|
||||
if last_online := cast(Optional[int], player.get("lastlogoff")):
|
||||
attrs["last_online"] = utc_from_timestamp(mktime(localtime(last_online)))
|
||||
if level := self.coordinator.data[self.entity_description.key]["level"]:
|
||||
attrs["level"] = level
|
||||
|
Loading…
x
Reference in New Issue
Block a user