mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
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:
parent
3fd863bd7c
commit
9fb01f3956
@ -10,12 +10,11 @@ from homeassistant.components.cover import (
|
|||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import LinearDevice, LinearUpdateCoordinator
|
from .coordinator import LinearUpdateCoordinator
|
||||||
|
from .entity import LinearEntity
|
||||||
|
|
||||||
SUPPORTED_SUBDEVICES = ["GDO"]
|
SUPPORTED_SUBDEVICES = ["GDO"]
|
||||||
PARALLEL_UPDATES = 1
|
PARALLEL_UPDATES = 1
|
||||||
@ -31,49 +30,20 @@ async def async_setup_entry(
|
|||||||
coordinator: LinearUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator: LinearUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
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 device_id, device_data in coordinator.data.items()
|
||||||
for sub_device_id in device_data.subdevices
|
for sub_device_id in device_data.subdevices
|
||||||
if sub_device_id in SUPPORTED_SUBDEVICES
|
if sub_device_id in SUPPORTED_SUBDEVICES
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class LinearCoverEntity(CoordinatorEntity[LinearUpdateCoordinator], CoverEntity):
|
class LinearCoverEntity(LinearEntity, CoverEntity):
|
||||||
"""Representation of a Linear cover."""
|
"""Representation of a Linear cover."""
|
||||||
|
|
||||||
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
||||||
_attr_has_entity_name = True
|
|
||||||
_attr_name = None
|
_attr_name = None
|
||||||
_attr_device_class = CoverDeviceClass.GARAGE
|
_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
|
@property
|
||||||
def is_closed(self) -> bool:
|
def is_closed(self) -> bool:
|
||||||
"""Return if cover is closed."""
|
"""Return if cover is closed."""
|
||||||
|
43
homeassistant/components/linear_garage_door/entity.py
Normal file
43
homeassistant/components/linear_garage_door/entity.py
Normal 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]
|
Loading…
x
Reference in New Issue
Block a user