From ed2c30b83065b166754993d805545febeade59b7 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 14 May 2024 11:41:27 +0200 Subject: [PATCH] Move abode base entities to separate module (#117417) --- homeassistant/components/abode/__init__.py | 112 +---------------- .../components/abode/alarm_control_panel.py | 3 +- .../components/abode/binary_sensor.py | 3 +- homeassistant/components/abode/camera.py | 3 +- homeassistant/components/abode/cover.py | 3 +- homeassistant/components/abode/entity.py | 115 ++++++++++++++++++ homeassistant/components/abode/light.py | 3 +- homeassistant/components/abode/lock.py | 3 +- homeassistant/components/abode/sensor.py | 3 +- homeassistant/components/abode/switch.py | 3 +- 10 files changed, 133 insertions(+), 118 deletions(-) create mode 100644 homeassistant/components/abode/entity.py diff --git a/homeassistant/components/abode/__init__.py b/homeassistant/components/abode/__init__.py index a27c2d93ead..76d4e5a5351 100644 --- a/homeassistant/components/abode/__init__.py +++ b/homeassistant/components/abode/__init__.py @@ -5,9 +5,7 @@ from __future__ import annotations from dataclasses import dataclass, field from functools import partial -from jaraco.abode.automation import Automation as AbodeAuto from jaraco.abode.client import Client as Abode -from jaraco.abode.devices.base import Device as AbodeDev from jaraco.abode.exceptions import ( AuthenticationException as AbodeAuthenticationException, Exception as AbodeException, @@ -29,11 +27,10 @@ from homeassistant.const import ( ) from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, ServiceCall from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from homeassistant.helpers import config_validation as cv, entity -from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.dispatcher import dispatcher_send -from .const import ATTRIBUTION, CONF_POLLING, DOMAIN, LOGGER +from .const import CONF_POLLING, DOMAIN, LOGGER SERVICE_SETTINGS = "change_setting" SERVICE_CAPTURE_IMAGE = "capture_image" @@ -247,108 +244,3 @@ def setup_abode_events(hass: HomeAssistant) -> None: hass.data[DOMAIN].abode.events.add_event_callback( event, partial(event_callback, event) ) - - -class AbodeEntity(entity.Entity): - """Representation of an Abode entity.""" - - _attr_attribution = ATTRIBUTION - _attr_has_entity_name = True - - def __init__(self, data: AbodeSystem) -> None: - """Initialize Abode entity.""" - self._data = data - self._attr_should_poll = data.polling - - async def async_added_to_hass(self) -> None: - """Subscribe to Abode connection status updates.""" - await self.hass.async_add_executor_job( - self._data.abode.events.add_connection_status_callback, - self.unique_id, - self._update_connection_status, - ) - - self.hass.data[DOMAIN].entity_ids.add(self.entity_id) - - async def async_will_remove_from_hass(self) -> None: - """Unsubscribe from Abode connection status updates.""" - await self.hass.async_add_executor_job( - self._data.abode.events.remove_connection_status_callback, self.unique_id - ) - - def _update_connection_status(self) -> None: - """Update the entity available property.""" - self._attr_available = self._data.abode.events.connected - self.schedule_update_ha_state() - - -class AbodeDevice(AbodeEntity): - """Representation of an Abode device.""" - - def __init__(self, data: AbodeSystem, device: AbodeDev) -> None: - """Initialize Abode device.""" - super().__init__(data) - self._device = device - self._attr_unique_id = device.uuid - - async def async_added_to_hass(self) -> None: - """Subscribe to device events.""" - await super().async_added_to_hass() - await self.hass.async_add_executor_job( - self._data.abode.events.add_device_callback, - self._device.id, - self._update_callback, - ) - - async def async_will_remove_from_hass(self) -> None: - """Unsubscribe from device events.""" - await super().async_will_remove_from_hass() - await self.hass.async_add_executor_job( - self._data.abode.events.remove_all_device_callbacks, self._device.id - ) - - def update(self) -> None: - """Update device state.""" - self._device.refresh() - - @property - def extra_state_attributes(self) -> dict[str, str]: - """Return the state attributes.""" - return { - "device_id": self._device.id, - "battery_low": self._device.battery_low, - "no_response": self._device.no_response, - "device_type": self._device.type, - } - - @property - def device_info(self) -> DeviceInfo: - """Return device registry information for this entity.""" - return DeviceInfo( - identifiers={(DOMAIN, self._device.id)}, - manufacturer="Abode", - model=self._device.type, - name=self._device.name, - ) - - def _update_callback(self, device: AbodeDev) -> None: - """Update the device state.""" - self.schedule_update_ha_state() - - -class AbodeAutomation(AbodeEntity): - """Representation of an Abode automation.""" - - def __init__(self, data: AbodeSystem, automation: AbodeAuto) -> None: - """Initialize for Abode automation.""" - super().__init__(data) - self._automation = automation - self._attr_name = automation.name - self._attr_unique_id = automation.automation_id - self._attr_extra_state_attributes = { - "type": "CUE automation", - } - - def update(self) -> None: - """Update automation state.""" - self._automation.refresh() diff --git a/homeassistant/components/abode/alarm_control_panel.py b/homeassistant/components/abode/alarm_control_panel.py index 333462a4d9f..b58a4757785 100644 --- a/homeassistant/components/abode/alarm_control_panel.py +++ b/homeassistant/components/abode/alarm_control_panel.py @@ -17,8 +17,9 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AbodeDevice, AbodeSystem +from . import AbodeSystem from .const import DOMAIN +from .entity import AbodeDevice async def async_setup_entry( diff --git a/homeassistant/components/abode/binary_sensor.py b/homeassistant/components/abode/binary_sensor.py index 4968d5378e1..1bccbf61701 100644 --- a/homeassistant/components/abode/binary_sensor.py +++ b/homeassistant/components/abode/binary_sensor.py @@ -22,8 +22,9 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.enum import try_parse_enum -from . import AbodeDevice, AbodeSystem +from . import AbodeSystem from .const import DOMAIN +from .entity import AbodeDevice async def async_setup_entry( diff --git a/homeassistant/components/abode/camera.py b/homeassistant/components/abode/camera.py index 8ffa90a9b82..57fcbf1fca4 100644 --- a/homeassistant/components/abode/camera.py +++ b/homeassistant/components/abode/camera.py @@ -19,8 +19,9 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util import Throttle -from . import AbodeDevice, AbodeSystem +from . import AbodeSystem from .const import DOMAIN, LOGGER +from .entity import AbodeDevice MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=90) diff --git a/homeassistant/components/abode/cover.py b/homeassistant/components/abode/cover.py index e3fbb1a5b8f..96270cfd966 100644 --- a/homeassistant/components/abode/cover.py +++ b/homeassistant/components/abode/cover.py @@ -10,8 +10,9 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AbodeDevice, AbodeSystem +from . import AbodeSystem from .const import DOMAIN +from .entity import AbodeDevice async def async_setup_entry( diff --git a/homeassistant/components/abode/entity.py b/homeassistant/components/abode/entity.py new file mode 100644 index 00000000000..adbb68d86c6 --- /dev/null +++ b/homeassistant/components/abode/entity.py @@ -0,0 +1,115 @@ +"""Support for Abode Security System entities.""" + +from jaraco.abode.automation import Automation as AbodeAuto +from jaraco.abode.devices.base import Device as AbodeDev + +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.entity import Entity + +from . import AbodeSystem +from .const import ATTRIBUTION, DOMAIN + + +class AbodeEntity(Entity): + """Representation of an Abode entity.""" + + _attr_attribution = ATTRIBUTION + _attr_has_entity_name = True + + def __init__(self, data: AbodeSystem) -> None: + """Initialize Abode entity.""" + self._data = data + self._attr_should_poll = data.polling + + async def async_added_to_hass(self) -> None: + """Subscribe to Abode connection status updates.""" + await self.hass.async_add_executor_job( + self._data.abode.events.add_connection_status_callback, + self.unique_id, + self._update_connection_status, + ) + + self.hass.data[DOMAIN].entity_ids.add(self.entity_id) + + async def async_will_remove_from_hass(self) -> None: + """Unsubscribe from Abode connection status updates.""" + await self.hass.async_add_executor_job( + self._data.abode.events.remove_connection_status_callback, self.unique_id + ) + + def _update_connection_status(self) -> None: + """Update the entity available property.""" + self._attr_available = self._data.abode.events.connected + self.schedule_update_ha_state() + + +class AbodeDevice(AbodeEntity): + """Representation of an Abode device.""" + + def __init__(self, data: AbodeSystem, device: AbodeDev) -> None: + """Initialize Abode device.""" + super().__init__(data) + self._device = device + self._attr_unique_id = device.uuid + + async def async_added_to_hass(self) -> None: + """Subscribe to device events.""" + await super().async_added_to_hass() + await self.hass.async_add_executor_job( + self._data.abode.events.add_device_callback, + self._device.id, + self._update_callback, + ) + + async def async_will_remove_from_hass(self) -> None: + """Unsubscribe from device events.""" + await super().async_will_remove_from_hass() + await self.hass.async_add_executor_job( + self._data.abode.events.remove_all_device_callbacks, self._device.id + ) + + def update(self) -> None: + """Update device state.""" + self._device.refresh() + + @property + def extra_state_attributes(self) -> dict[str, str]: + """Return the state attributes.""" + return { + "device_id": self._device.id, + "battery_low": self._device.battery_low, + "no_response": self._device.no_response, + "device_type": self._device.type, + } + + @property + def device_info(self) -> DeviceInfo: + """Return device registry information for this entity.""" + return DeviceInfo( + identifiers={(DOMAIN, self._device.id)}, + manufacturer="Abode", + model=self._device.type, + name=self._device.name, + ) + + def _update_callback(self, device: AbodeDev) -> None: + """Update the device state.""" + self.schedule_update_ha_state() + + +class AbodeAutomation(AbodeEntity): + """Representation of an Abode automation.""" + + def __init__(self, data: AbodeSystem, automation: AbodeAuto) -> None: + """Initialize for Abode automation.""" + super().__init__(data) + self._automation = automation + self._attr_name = automation.name + self._attr_unique_id = automation.automation_id + self._attr_extra_state_attributes = { + "type": "CUE automation", + } + + def update(self) -> None: + """Update automation state.""" + self._automation.refresh() diff --git a/homeassistant/components/abode/light.py b/homeassistant/components/abode/light.py index 188d3c18e40..83f00e417ad 100644 --- a/homeassistant/components/abode/light.py +++ b/homeassistant/components/abode/light.py @@ -23,8 +23,9 @@ from homeassistant.util.color import ( color_temperature_mired_to_kelvin, ) -from . import AbodeDevice, AbodeSystem +from . import AbodeSystem from .const import DOMAIN +from .entity import AbodeDevice async def async_setup_entry( diff --git a/homeassistant/components/abode/lock.py b/homeassistant/components/abode/lock.py index 1135d3c3b36..3a65fa4d6dc 100644 --- a/homeassistant/components/abode/lock.py +++ b/homeassistant/components/abode/lock.py @@ -10,8 +10,9 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AbodeDevice, AbodeSystem +from . import AbodeSystem from .const import DOMAIN +from .entity import AbodeDevice async def async_setup_entry( diff --git a/homeassistant/components/abode/sensor.py b/homeassistant/components/abode/sensor.py index 89e5cf574fb..b57b3e77abc 100644 --- a/homeassistant/components/abode/sensor.py +++ b/homeassistant/components/abode/sensor.py @@ -27,8 +27,9 @@ from homeassistant.const import LIGHT_LUX, PERCENTAGE, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AbodeDevice, AbodeSystem +from . import AbodeSystem from .const import DOMAIN +from .entity import AbodeDevice ABODE_TEMPERATURE_UNIT_HA_UNIT = { UNIT_FAHRENHEIT: UnitOfTemperature.FAHRENHEIT, diff --git a/homeassistant/components/abode/switch.py b/homeassistant/components/abode/switch.py index 9a33a04e341..64eb3529aab 100644 --- a/homeassistant/components/abode/switch.py +++ b/homeassistant/components/abode/switch.py @@ -13,8 +13,9 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AbodeAutomation, AbodeDevice, AbodeSystem +from . import AbodeSystem from .const import DOMAIN +from .entity import AbodeAutomation, AbodeDevice DEVICE_TYPES = [TYPE_SWITCH, TYPE_VALVE]