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

@ -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