Improve opengarage
generic typing (#84640)
This commit is contained in:
parent
235a34c10c
commit
3312a041fd
5 changed files with 45 additions and 22 deletions
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import opengarage
|
||||
|
||||
|
@ -11,6 +12,7 @@ from homeassistant.const import CONF_HOST, CONF_PORT, CONF_VERIFY_SSL, Platform
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import update_coordinator
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import CONF_DEVICE_KEY, DOMAIN
|
||||
|
||||
|
@ -50,7 +52,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
return unload_ok
|
||||
|
||||
|
||||
class OpenGarageDataUpdateCoordinator(update_coordinator.DataUpdateCoordinator):
|
||||
class OpenGarageDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
"""Class to manage fetching Opengarage data."""
|
||||
|
||||
def __init__(
|
||||
|
@ -69,7 +71,7 @@ class OpenGarageDataUpdateCoordinator(update_coordinator.DataUpdateCoordinator):
|
|||
update_interval=timedelta(seconds=5),
|
||||
)
|
||||
|
||||
async def _async_update_data(self) -> None:
|
||||
async def _async_update_data(self) -> dict[str, Any]:
|
||||
"""Fetch data."""
|
||||
data = await self.open_garage_connection.update_state()
|
||||
if data is None:
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorEntity,
|
||||
|
@ -11,6 +12,7 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import OpenGarageDataUpdateCoordinator
|
||||
from .const import DOMAIN
|
||||
from .entity import OpenGarageEntity
|
||||
|
||||
|
@ -28,12 +30,14 @@ async def async_setup_entry(
|
|||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the OpenGarage binary sensors."""
|
||||
open_garage_data_coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
open_garage_data_coordinator: OpenGarageDataUpdateCoordinator = hass.data[DOMAIN][
|
||||
entry.entry_id
|
||||
]
|
||||
async_add_entities(
|
||||
[
|
||||
OpenGarageBinarySensor(
|
||||
open_garage_data_coordinator,
|
||||
entry.unique_id,
|
||||
cast(str, entry.unique_id),
|
||||
description,
|
||||
)
|
||||
for description in SENSOR_TYPES
|
||||
|
@ -44,10 +48,15 @@ async def async_setup_entry(
|
|||
class OpenGarageBinarySensor(OpenGarageEntity, BinarySensorEntity):
|
||||
"""Representation of a OpenGarage binary sensor."""
|
||||
|
||||
def __init__(self, open_garage_data_coordinator, device_id, description):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: OpenGarageDataUpdateCoordinator,
|
||||
device_id: str,
|
||||
description: BinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the entity."""
|
||||
self._available = False
|
||||
super().__init__(open_garage_data_coordinator, device_id, description)
|
||||
super().__init__(coordinator, device_id, description)
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
from typing import Any, cast
|
||||
|
||||
from homeassistant.components.cover import (
|
||||
CoverDeviceClass,
|
||||
|
@ -14,6 +14,7 @@ from homeassistant.const import STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_O
|
|||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import OpenGarageDataUpdateCoordinator
|
||||
from .const import DOMAIN
|
||||
from .entity import OpenGarageEntity
|
||||
|
||||
|
@ -27,7 +28,7 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up the OpenGarage covers."""
|
||||
async_add_entities(
|
||||
[OpenGarageCover(hass.data[DOMAIN][entry.entry_id], entry.unique_id)]
|
||||
[OpenGarageCover(hass.data[DOMAIN][entry.entry_id], cast(str, entry.unique_id))]
|
||||
)
|
||||
|
||||
|
||||
|
@ -37,12 +38,14 @@ class OpenGarageCover(OpenGarageEntity, CoverEntity):
|
|||
_attr_device_class = CoverDeviceClass.GARAGE
|
||||
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
||||
|
||||
def __init__(self, open_garage_data_coordinator, device_id):
|
||||
def __init__(
|
||||
self, coordinator: OpenGarageDataUpdateCoordinator, device_id: str
|
||||
) -> None:
|
||||
"""Initialize the cover."""
|
||||
self._state = None
|
||||
self._state_before_move = None
|
||||
self._state: str | None = None
|
||||
self._state_before_move: str | None = None
|
||||
|
||||
super().__init__(open_garage_data_coordinator, device_id)
|
||||
super().__init__(coordinator, device_id)
|
||||
|
||||
@property
|
||||
def is_closed(self) -> bool | None:
|
||||
|
@ -87,7 +90,7 @@ class OpenGarageCover(OpenGarageEntity, CoverEntity):
|
|||
status = self.coordinator.data
|
||||
|
||||
self._attr_name = status["name"]
|
||||
state = STATES_MAP.get(status.get("door"))
|
||||
state = STATES_MAP.get(status.get("door")) # type: ignore[arg-type]
|
||||
if self._state_before_move is not None:
|
||||
if self._state_before_move != state:
|
||||
self._state = state
|
||||
|
|
|
@ -1,17 +1,23 @@
|
|||
"""Entity for the opengarage.io component."""
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from . import DOMAIN
|
||||
from . import DOMAIN, OpenGarageDataUpdateCoordinator
|
||||
|
||||
|
||||
class OpenGarageEntity(CoordinatorEntity):
|
||||
class OpenGarageEntity(CoordinatorEntity[OpenGarageDataUpdateCoordinator]):
|
||||
"""Representation of a OpenGarage entity."""
|
||||
|
||||
def __init__(self, open_garage_data_coordinator, device_id, description=None):
|
||||
def __init__(
|
||||
self,
|
||||
open_garage_data_coordinator: OpenGarageDataUpdateCoordinator,
|
||||
device_id: str,
|
||||
description: EntityDescription | None = None,
|
||||
) -> None:
|
||||
"""Initialize the entity."""
|
||||
super().__init__(open_garage_data_coordinator)
|
||||
|
||||
|
@ -35,9 +41,9 @@ class OpenGarageEntity(CoordinatorEntity):
|
|||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return the device_info of the device."""
|
||||
device_info = DeviceInfo(
|
||||
return DeviceInfo(
|
||||
configuration_url=self.coordinator.open_garage_connection.device_url,
|
||||
connections={(CONNECTION_NETWORK_MAC, self.coordinator.data["mac"])},
|
||||
identifiers={(DOMAIN, self._device_id)},
|
||||
|
@ -46,4 +52,3 @@ class OpenGarageEntity(CoordinatorEntity):
|
|||
suggested_area="Garage",
|
||||
sw_version=self.coordinator.data["fwv"],
|
||||
)
|
||||
return device_info
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
|
@ -20,6 +21,7 @@ from homeassistant.core import HomeAssistant, callback
|
|||
from homeassistant.helpers.entity import EntityCategory
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import OpenGarageDataUpdateCoordinator
|
||||
from .const import DOMAIN
|
||||
from .entity import OpenGarageEntity
|
||||
|
||||
|
@ -59,12 +61,14 @@ async def async_setup_entry(
|
|||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the OpenGarage sensors."""
|
||||
open_garage_data_coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
open_garage_data_coordinator: OpenGarageDataUpdateCoordinator = hass.data[DOMAIN][
|
||||
entry.entry_id
|
||||
]
|
||||
async_add_entities(
|
||||
[
|
||||
OpenGarageSensor(
|
||||
open_garage_data_coordinator,
|
||||
entry.unique_id,
|
||||
cast(str, entry.unique_id),
|
||||
description,
|
||||
)
|
||||
for description in SENSOR_TYPES
|
||||
|
|
Loading…
Add table
Reference in a new issue