Move gogogate2 base entity to separate module (#126485)
This commit is contained in:
parent
16221cfbbd
commit
ffa7e5a504
4 changed files with 75 additions and 61 deletions
|
@ -14,7 +14,7 @@ from ismartgate import (
|
|||
ISmartGateApi,
|
||||
ISmartGateInfoResponse,
|
||||
)
|
||||
from ismartgate.common import AbstractDoor, get_door_by_id
|
||||
from ismartgate.common import AbstractDoor
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
|
@ -24,11 +24,10 @@ from homeassistant.const import (
|
|||
CONF_USERNAME,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.httpx_client import get_async_client
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity, UpdateFailed
|
||||
from homeassistant.helpers.update_coordinator import UpdateFailed
|
||||
|
||||
from .const import DATA_UPDATE_COORDINATOR, DEVICE_TYPE_ISMARTGATE, DOMAIN, MANUFACTURER
|
||||
from .const import DATA_UPDATE_COORDINATOR, DEVICE_TYPE_ISMARTGATE, DOMAIN
|
||||
from .coordinator import DeviceDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -42,61 +41,6 @@ class StateData(NamedTuple):
|
|||
door: AbstractDoor | None
|
||||
|
||||
|
||||
class GoGoGate2Entity(CoordinatorEntity[DeviceDataUpdateCoordinator]):
|
||||
"""Base class for gogogate2 entities."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
data_update_coordinator: DeviceDataUpdateCoordinator,
|
||||
door: AbstractDoor,
|
||||
unique_id: str,
|
||||
) -> None:
|
||||
"""Initialize gogogate2 base entity."""
|
||||
super().__init__(data_update_coordinator)
|
||||
self._config_entry = config_entry
|
||||
self._door = door
|
||||
self._door_id = door.door_id
|
||||
self._api = data_update_coordinator.api
|
||||
self._attr_unique_id = unique_id
|
||||
|
||||
@property
|
||||
def door(self) -> AbstractDoor:
|
||||
"""Return the door object."""
|
||||
door = get_door_by_id(self._door.door_id, self.coordinator.data)
|
||||
self._door = door or self._door
|
||||
return self._door
|
||||
|
||||
@property
|
||||
def door_status(self) -> AbstractDoor:
|
||||
"""Return the door with status."""
|
||||
data = self.coordinator.data
|
||||
door_with_statuses = self._api.async_get_door_statuses_from_info(data)
|
||||
return door_with_statuses[self._door_id]
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Device info for the controller."""
|
||||
data = self.coordinator.data
|
||||
if data.remoteaccessenabled:
|
||||
configuration_url = f"https://{data.remoteaccess}"
|
||||
else:
|
||||
configuration_url = f"http://{self._config_entry.data[CONF_IP_ADDRESS]}"
|
||||
return DeviceInfo(
|
||||
configuration_url=configuration_url,
|
||||
identifiers={(DOMAIN, str(self._config_entry.unique_id))},
|
||||
name=self._config_entry.title,
|
||||
manufacturer=MANUFACTURER,
|
||||
model=data.model,
|
||||
sw_version=data.firmwareversion,
|
||||
)
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return {"door_id": self._door_id}
|
||||
|
||||
|
||||
def get_data_update_coordinator(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
) -> DeviceDataUpdateCoordinator:
|
||||
|
|
|
@ -20,8 +20,9 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .common import GoGoGate2Entity, cover_unique_id, get_data_update_coordinator
|
||||
from .common import cover_unique_id, get_data_update_coordinator
|
||||
from .coordinator import DeviceDataUpdateCoordinator
|
||||
from .entity import GoGoGate2Entity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
|
|
68
homeassistant/components/gogogate2/entity.py
Normal file
68
homeassistant/components/gogogate2/entity.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
"""Common code for GogoGate2 component."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ismartgate.common import AbstractDoor, get_door_by_id
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_IP_ADDRESS
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN, MANUFACTURER
|
||||
from .coordinator import DeviceDataUpdateCoordinator
|
||||
|
||||
|
||||
class GoGoGate2Entity(CoordinatorEntity[DeviceDataUpdateCoordinator]):
|
||||
"""Base class for gogogate2 entities."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
data_update_coordinator: DeviceDataUpdateCoordinator,
|
||||
door: AbstractDoor,
|
||||
unique_id: str,
|
||||
) -> None:
|
||||
"""Initialize gogogate2 base entity."""
|
||||
super().__init__(data_update_coordinator)
|
||||
self._config_entry = config_entry
|
||||
self._door = door
|
||||
self._door_id = door.door_id
|
||||
self._api = data_update_coordinator.api
|
||||
self._attr_unique_id = unique_id
|
||||
|
||||
@property
|
||||
def door(self) -> AbstractDoor:
|
||||
"""Return the door object."""
|
||||
door = get_door_by_id(self._door.door_id, self.coordinator.data)
|
||||
self._door = door or self._door
|
||||
return self._door
|
||||
|
||||
@property
|
||||
def door_status(self) -> AbstractDoor:
|
||||
"""Return the door with status."""
|
||||
data = self.coordinator.data
|
||||
door_with_statuses = self._api.async_get_door_statuses_from_info(data)
|
||||
return door_with_statuses[self._door_id]
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Device info for the controller."""
|
||||
data = self.coordinator.data
|
||||
if data.remoteaccessenabled:
|
||||
configuration_url = f"https://{data.remoteaccess}"
|
||||
else:
|
||||
configuration_url = f"http://{self._config_entry.data[CONF_IP_ADDRESS]}"
|
||||
return DeviceInfo(
|
||||
configuration_url=configuration_url,
|
||||
identifiers={(DOMAIN, str(self._config_entry.unique_id))},
|
||||
name=self._config_entry.title,
|
||||
manufacturer=MANUFACTURER,
|
||||
model=data.model,
|
||||
sw_version=data.firmwareversion,
|
||||
)
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return {"door_id": self._door_id}
|
|
@ -16,8 +16,9 @@ from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .common import GoGoGate2Entity, get_data_update_coordinator, sensor_unique_id
|
||||
from .common import get_data_update_coordinator, sensor_unique_id
|
||||
from .coordinator import DeviceDataUpdateCoordinator
|
||||
from .entity import GoGoGate2Entity
|
||||
|
||||
SENSOR_ID_WIRED = "WIRE"
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue