Add typing to wsdot (#143117)

* increase wsdot typing

* remove Final types

* help out mypy

* simplify wsdot types

* minor wsdot type changes

* type wsdot state
This commit is contained in:
Jeremiah Paige 2025-05-13 01:43:03 -07:00 committed by GitHub
parent c121631fef
commit 2db60340c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -65,7 +65,7 @@ def setup_platform(
name = travel_time.get(CONF_NAME) or travel_time.get(CONF_ID) name = travel_time.get(CONF_NAME) or travel_time.get(CONF_ID)
sensors.append( sensors.append(
WashingtonStateTravelTimeSensor( WashingtonStateTravelTimeSensor(
name, config.get(CONF_API_KEY), travel_time.get(CONF_ID) name, config[CONF_API_KEY], travel_time.get(CONF_ID)
) )
) )
@ -82,20 +82,20 @@ class WashingtonStateTransportSensor(SensorEntity):
_attr_icon = ICON _attr_icon = ICON
def __init__(self, name, access_code): def __init__(self, name: str, access_code: str) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self._data = {} self._data: dict[str, str | int | None] = {}
self._access_code = access_code self._access_code = access_code
self._name = name self._name = name
self._state = None self._state: int | None = None
@property @property
def name(self): def name(self) -> str:
"""Return the name of the sensor.""" """Return the name of the sensor."""
return self._name return self._name
@property @property
def native_value(self): def native_value(self) -> int | None:
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._state return self._state
@ -106,7 +106,7 @@ class WashingtonStateTravelTimeSensor(WashingtonStateTransportSensor):
_attr_attribution = ATTRIBUTION _attr_attribution = ATTRIBUTION
_attr_native_unit_of_measurement = UnitOfTime.MINUTES _attr_native_unit_of_measurement = UnitOfTime.MINUTES
def __init__(self, name, access_code, travel_time_id): def __init__(self, name: str, access_code: str, travel_time_id: str) -> None:
"""Construct a travel time sensor.""" """Construct a travel time sensor."""
self._travel_time_id = travel_time_id self._travel_time_id = travel_time_id
WashingtonStateTransportSensor.__init__(self, name, access_code) WashingtonStateTransportSensor.__init__(self, name, access_code)
@ -123,13 +123,17 @@ class WashingtonStateTravelTimeSensor(WashingtonStateTransportSensor):
_LOGGER.warning("Invalid response from WSDOT API") _LOGGER.warning("Invalid response from WSDOT API")
else: else:
self._data = response.json() self._data = response.json()
self._state = self._data.get(ATTR_CURRENT_TIME) _state = self._data.get(ATTR_CURRENT_TIME)
if not isinstance(_state, int):
self._state = None
else:
self._state = _state
@property @property
def extra_state_attributes(self) -> dict[str, Any] | None: def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return other details about the sensor state.""" """Return other details about the sensor state."""
if self._data is not None: if self._data is not None:
attrs = {} attrs: dict[str, str | int | None | datetime] = {}
for key in ( for key in (
ATTR_AVG_TIME, ATTR_AVG_TIME,
ATTR_NAME, ATTR_NAME,
@ -144,12 +148,15 @@ class WashingtonStateTravelTimeSensor(WashingtonStateTransportSensor):
return None return None
def _parse_wsdot_timestamp(timestamp): def _parse_wsdot_timestamp(timestamp: Any) -> datetime | None:
"""Convert WSDOT timestamp to datetime.""" """Convert WSDOT timestamp to datetime."""
if not timestamp: if not isinstance(timestamp, str):
return None return None
# ex: Date(1485040200000-0800) # ex: Date(1485040200000-0800)
milliseconds, tzone = re.search(r"Date\((\d+)([+-]\d\d)\d\d\)", timestamp).groups() timestamp_parts = re.search(r"Date\((\d+)([+-]\d\d)\d\d\)", timestamp)
if timestamp_parts is None:
return None
milliseconds, tzone = timestamp_parts.groups()
return datetime.fromtimestamp( return datetime.fromtimestamp(
int(milliseconds) / 1000, tz=timezone(timedelta(hours=int(tzone))) int(milliseconds) / 1000, tz=timezone(timedelta(hours=int(tzone)))
) )