Convert Linear to use a base entity (#116133)

* Convert Linear to use a base entity

* Convert Linear to use a base entity

* Remove str cast in coordinator

* More minor fixes
This commit is contained in:
IceBotYT 2024-04-27 17:11:52 -04:00 committed by GitHub
parent 3fd863bd7c
commit 9fb01f3956
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 34 deletions

View File

@ -10,12 +10,11 @@ from homeassistant.components.cover import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import LinearDevice, LinearUpdateCoordinator
from .coordinator import LinearUpdateCoordinator
from .entity import LinearEntity
SUPPORTED_SUBDEVICES = ["GDO"]
PARALLEL_UPDATES = 1
@ -31,49 +30,20 @@ async def async_setup_entry(
coordinator: LinearUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities(
LinearCoverEntity(coordinator, device_id, sub_device_id)
LinearCoverEntity(coordinator, device_id, device_data.name, sub_device_id)
for device_id, device_data in coordinator.data.items()
for sub_device_id in device_data.subdevices
if sub_device_id in SUPPORTED_SUBDEVICES
)
class LinearCoverEntity(CoordinatorEntity[LinearUpdateCoordinator], CoverEntity):
class LinearCoverEntity(LinearEntity, CoverEntity):
"""Representation of a Linear cover."""
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
_attr_has_entity_name = True
_attr_name = None
_attr_device_class = CoverDeviceClass.GARAGE
def __init__(
self,
coordinator: LinearUpdateCoordinator,
device_id: str,
sub_device_id: str,
) -> None:
"""Init with device ID and name."""
super().__init__(coordinator)
self._device_id = device_id
self._sub_device_id = sub_device_id
self._attr_unique_id = f"{device_id}-{sub_device_id}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, sub_device_id)},
name=self.linear_device.name,
manufacturer="Linear",
model="Garage Door Opener",
)
@property
def linear_device(self) -> LinearDevice:
"""Return the Linear device."""
return self.coordinator.data[self._device_id]
@property
def sub_device(self) -> dict[str, str]:
"""Return the subdevice."""
return self.linear_device.subdevices[self._sub_device_id]
@property
def is_closed(self) -> bool:
"""Return if cover is closed."""

View File

@ -0,0 +1,43 @@
"""Base entity for Linear."""
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import LinearDevice, LinearUpdateCoordinator
class LinearEntity(CoordinatorEntity[LinearUpdateCoordinator]):
"""Common base for Linear entities."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: LinearUpdateCoordinator,
device_id: str,
device_name: str,
sub_device_id: str,
) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self._attr_unique_id = f"{device_id}-{sub_device_id}"
self._device_id = device_id
self._sub_device_id = sub_device_id
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device_id)},
name=device_name,
manufacturer="Linear",
model="Garage Door Opener",
)
@property
def linear_device(self) -> LinearDevice:
"""Return the Linear device."""
return self.coordinator.data[self._device_id]
@property
def sub_device(self) -> dict[str, str]:
"""Return the subdevice."""
return self.linear_device.subdevices[self._sub_device_id]