Differ device and domain entities in bosch_shc integration (#67957)

This commit is contained in:
Thomas Schamm 2022-06-29 07:08:16 +02:00 committed by GitHub
parent 596f60bdb5
commit 90c68085be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 18 deletions

View File

@ -19,7 +19,12 @@ from .const import (
DOMAIN, DOMAIN,
) )
PLATFORMS = [Platform.BINARY_SENSOR, Platform.COVER, Platform.SENSOR, Platform.SWITCH] PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.COVER,
Platform.SENSOR,
Platform.SWITCH,
]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -1,5 +1,7 @@
"""Bosch Smart Home Controller base entity.""" """Bosch Smart Home Controller base entity."""
from boschshcpy.device import SHCDevice from __future__ import annotations
from boschshcpy import SHCDevice, SHCIntrusionSystem
from homeassistant.helpers.device_registry import async_get as get_dev_reg from homeassistant.helpers.device_registry import async_get as get_dev_reg
from homeassistant.helpers.entity import DeviceInfo, Entity from homeassistant.helpers.entity import DeviceInfo, Entity
@ -7,7 +9,7 @@ from homeassistant.helpers.entity import DeviceInfo, Entity
from .const import DOMAIN from .const import DOMAIN
async def async_remove_devices(hass, entity, entry_id): async def async_remove_devices(hass, entity, entry_id) -> None:
"""Get item that is removed from session.""" """Get item that is removed from session."""
dev_registry = get_dev_reg(hass) dev_registry = get_dev_reg(hass)
device = dev_registry.async_get_device( device = dev_registry.async_get_device(
@ -17,16 +19,42 @@ async def async_remove_devices(hass, entity, entry_id):
dev_registry.async_update_device(device.id, remove_config_entry_id=entry_id) dev_registry.async_update_device(device.id, remove_config_entry_id=entry_id)
class SHCEntity(Entity): class SHCBaseEntity(Entity):
"""Representation of a SHC base entity.""" """Base representation of a SHC entity."""
_attr_should_poll = False _attr_should_poll = False
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None: def __init__(
self, device: SHCDevice | SHCIntrusionSystem, parent_id: str, entry_id: str
) -> None:
"""Initialize the generic SHC device.""" """Initialize the generic SHC device."""
self._device = device self._device = device
self._entry_id = entry_id self._entry_id = entry_id
self._attr_name = device.name self._attr_name = device.name
async def async_added_to_hass(self) -> None:
"""Subscribe to SHC events."""
await super().async_added_to_hass()
def on_state_changed() -> None:
if self._device.deleted:
self.hass.add_job(async_remove_devices(self.hass, self, self._entry_id))
else:
self.schedule_update_ha_state()
self._device.subscribe_callback(self.entity_id, on_state_changed)
async def async_will_remove_from_hass(self) -> None:
"""Unsubscribe from SHC events."""
await super().async_will_remove_from_hass()
self._device.unsubscribe_callback(self.entity_id)
class SHCEntity(SHCBaseEntity):
"""Representation of a SHC device entity."""
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize generic SHC device."""
self._attr_unique_id = device.serial self._attr_unique_id = device.serial
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device.id)}, identifiers={(DOMAIN, device.id)},
@ -40,32 +68,51 @@ class SHCEntity(Entity):
else parent_id, else parent_id,
), ),
) )
super().__init__(device=device, parent_id=parent_id, entry_id=entry_id)
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Subscribe to SHC events.""" """Subscribe to SHC events."""
await super().async_added_to_hass() await super().async_added_to_hass()
def on_state_changed(): def on_state_changed() -> None:
self.schedule_update_ha_state() self.schedule_update_ha_state()
def update_entity_information():
if self._device.deleted:
self.hass.add_job(async_remove_devices(self.hass, self, self._entry_id))
else:
self.schedule_update_ha_state()
for service in self._device.device_services: for service in self._device.device_services:
service.subscribe_callback(self.entity_id, on_state_changed) service.subscribe_callback(self.entity_id, on_state_changed)
self._device.subscribe_callback(self.entity_id, update_entity_information)
async def async_will_remove_from_hass(self): async def async_will_remove_from_hass(self) -> None:
"""Unsubscribe from SHC events.""" """Unsubscribe from SHC events."""
await super().async_will_remove_from_hass() await super().async_will_remove_from_hass()
for service in self._device.device_services: for service in self._device.device_services:
service.unsubscribe_callback(self.entity_id) service.unsubscribe_callback(self.entity_id)
self._device.unsubscribe_callback(self.entity_id)
@property @property
def available(self): def available(self) -> bool:
"""Return false if status is unavailable.""" """Return false if status is unavailable."""
return self._device.status == "AVAILABLE" return self._device.status == "AVAILABLE"
class SHCDomainEntity(SHCBaseEntity):
"""Representation of a SHC domain service entity."""
def __init__(
self, domain: SHCIntrusionSystem, parent_id: str, entry_id: str
) -> None:
"""Initialize the generic SHC device."""
self._attr_unique_id = domain.id
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, domain.id)},
manufacturer=domain.manufacturer,
model=domain.device_model,
name=domain.name,
via_device=(
DOMAIN,
parent_id,
),
)
super().__init__(device=domain, parent_id=parent_id, entry_id=entry_id)
@property
def available(self) -> bool:
"""Return false if status is unavailable."""
return self._device.system_availability