From 4d44d60e734069d0e9c1c282bc308adbd4ed9a4b Mon Sep 17 00:00:00 2001 From: David Knowles Date: Mon, 22 May 2023 09:47:32 -0400 Subject: [PATCH] Move HydrawiseEntity into entity.py (#93359) --- .../components/hydrawise/__init__.py | 35 +----------------- .../components/hydrawise/binary_sensor.py | 2 +- homeassistant/components/hydrawise/entity.py | 37 +++++++++++++++++++ homeassistant/components/hydrawise/sensor.py | 2 +- homeassistant/components/hydrawise/switch.py | 2 +- 5 files changed, 42 insertions(+), 36 deletions(-) create mode 100644 homeassistant/components/hydrawise/entity.py diff --git a/homeassistant/components/hydrawise/__init__.py b/homeassistant/components/hydrawise/__init__.py index 8f1217e25a1..ba561a14f82 100644 --- a/homeassistant/components/hydrawise/__init__.py +++ b/homeassistant/components/hydrawise/__init__.py @@ -6,10 +6,9 @@ import voluptuous as vol from homeassistant.components import persistent_notification from homeassistant.const import CONF_ACCESS_TOKEN, CONF_SCAN_INTERVAL -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import HomeAssistant import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send -from homeassistant.helpers.entity import Entity, EntityDescription +from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.helpers.event import track_time_interval from homeassistant.helpers.typing import ConfigType @@ -73,33 +72,3 @@ class HydrawiseHub: def __init__(self, data): """Initialize the entity.""" self.data = data - - -class HydrawiseEntity(Entity): - """Entity class for Hydrawise devices.""" - - _attr_attribution = "Data provided by hydrawise.com" - - def __init__(self, data, description: EntityDescription) -> None: - """Initialize the Hydrawise entity.""" - self.entity_description = description - self.data = data - self._attr_name = f"{self.data['name']} {description.name}" - - async def async_added_to_hass(self): - """Register callbacks.""" - self.async_on_remove( - async_dispatcher_connect( - self.hass, SIGNAL_UPDATE_HYDRAWISE, self._update_callback - ) - ) - - @callback - def _update_callback(self): - """Call update method.""" - self.async_schedule_update_ha_state(True) - - @property - def extra_state_attributes(self): - """Return the state attributes.""" - return {"identifier": self.data.get("relay")} diff --git a/homeassistant/components/hydrawise/binary_sensor.py b/homeassistant/components/hydrawise/binary_sensor.py index 832820fbcc7..93594d71436 100644 --- a/homeassistant/components/hydrawise/binary_sensor.py +++ b/homeassistant/components/hydrawise/binary_sensor.py @@ -15,8 +15,8 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from . import HydrawiseEntity from .const import DATA_HYDRAWISE, LOGGER +from .entity import HydrawiseEntity BINARY_SENSOR_STATUS = BinarySensorEntityDescription( key="status", diff --git a/homeassistant/components/hydrawise/entity.py b/homeassistant/components/hydrawise/entity.py new file mode 100644 index 00000000000..5c54c1ee580 --- /dev/null +++ b/homeassistant/components/hydrawise/entity.py @@ -0,0 +1,37 @@ +"""Base classes for Hydrawise entities.""" + +from homeassistant.core import callback +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import Entity, EntityDescription + +from .const import SIGNAL_UPDATE_HYDRAWISE + + +class HydrawiseEntity(Entity): + """Entity class for Hydrawise devices.""" + + _attr_attribution = "Data provided by hydrawise.com" + + def __init__(self, data, description: EntityDescription) -> None: + """Initialize the Hydrawise entity.""" + self.entity_description = description + self.data = data + self._attr_name = f"{self.data['name']} {description.name}" + + async def async_added_to_hass(self): + """Register callbacks.""" + self.async_on_remove( + async_dispatcher_connect( + self.hass, SIGNAL_UPDATE_HYDRAWISE, self._update_callback + ) + ) + + @callback + def _update_callback(self): + """Call update method.""" + self.async_schedule_update_ha_state(True) + + @property + def extra_state_attributes(self): + """Return the state attributes.""" + return {"identifier": self.data.get("relay")} diff --git a/homeassistant/components/hydrawise/sensor.py b/homeassistant/components/hydrawise/sensor.py index 0b765d5d338..2cec1309ec9 100644 --- a/homeassistant/components/hydrawise/sensor.py +++ b/homeassistant/components/hydrawise/sensor.py @@ -16,8 +16,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util import dt -from . import HydrawiseEntity from .const import DATA_HYDRAWISE, LOGGER +from .entity import HydrawiseEntity SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( diff --git a/homeassistant/components/hydrawise/switch.py b/homeassistant/components/hydrawise/switch.py index f075b334cd5..ac9b0d27025 100644 --- a/homeassistant/components/hydrawise/switch.py +++ b/homeassistant/components/hydrawise/switch.py @@ -17,7 +17,6 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from . import HydrawiseEntity from .const import ( ALLOWED_WATERING_TIME, CONF_WATERING_TIME, @@ -25,6 +24,7 @@ from .const import ( DEFAULT_WATERING_TIME, LOGGER, ) +from .entity import HydrawiseEntity SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = ( SwitchEntityDescription(