From fbe2228c3fa45336c2ee98b53a3d87159fddf232 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Sun, 27 Aug 2023 20:09:10 +0200 Subject: [PATCH] Extract Ambient Station base entity to separate file (#99142) * Extract Ambient Station entity to separate file * Add to coveragerc --- .coveragerc | 1 + .../components/ambient_station/__init__.py | 66 +---------------- .../ambient_station/binary_sensor.py | 2 +- .../components/ambient_station/entity.py | 70 +++++++++++++++++++ .../components/ambient_station/sensor.py | 3 +- 5 files changed, 75 insertions(+), 67 deletions(-) create mode 100644 homeassistant/components/ambient_station/entity.py diff --git a/.coveragerc b/.coveragerc index 5753bc13195..5adb465509a 100644 --- a/.coveragerc +++ b/.coveragerc @@ -57,6 +57,7 @@ omit = homeassistant/components/ambiclimate/climate.py homeassistant/components/ambient_station/__init__.py homeassistant/components/ambient_station/binary_sensor.py + homeassistant/components/ambient_station/entity.py homeassistant/components/ambient_station/sensor.py homeassistant/components/amcrest/* homeassistant/components/ampio/* diff --git a/homeassistant/components/ambient_station/__init__.py b/homeassistant/components/ambient_station/__init__.py index e8c71fcad7a..1718b559fde 100644 --- a/homeassistant/components/ambient_station/__init__.py +++ b/homeassistant/components/ambient_station/__init__.py @@ -5,7 +5,6 @@ from typing import Any from aioambient import Websocket from aioambient.errors import WebsocketError -from aioambient.util import get_public_device_id from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -19,12 +18,7 @@ from homeassistant.core import Event, HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_validation as cv import homeassistant.helpers.device_registry as dr -from homeassistant.helpers.device_registry import DeviceInfo -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) -from homeassistant.helpers.entity import Entity, EntityDescription +from homeassistant.helpers.dispatcher import async_dispatcher_send import homeassistant.helpers.entity_registry as er from .const import ( @@ -198,61 +192,3 @@ class AmbientStation: async def ws_disconnect(self) -> None: """Disconnect from the websocket.""" await self.websocket.disconnect() - - -class AmbientWeatherEntity(Entity): - """Define a base Ambient PWS entity.""" - - _attr_has_entity_name = True - _attr_should_poll = False - - def __init__( - self, - ambient: AmbientStation, - mac_address: str, - station_name: str, - description: EntityDescription, - ) -> None: - """Initialize the entity.""" - self._ambient = ambient - - public_device_id = get_public_device_id(mac_address) - self._attr_device_info = DeviceInfo( - configuration_url=( - f"https://ambientweather.net/dashboard/{public_device_id}" - ), - identifiers={(DOMAIN, mac_address)}, - manufacturer="Ambient Weather", - name=station_name.capitalize(), - ) - - self._attr_unique_id = f"{mac_address}_{description.key}" - self._mac_address = mac_address - self.entity_description = description - - @callback - def _async_update(self) -> None: - """Update the state.""" - last_data = self._ambient.stations[self._mac_address][ATTR_LAST_DATA] - key = self.entity_description.key - available_key = TYPE_SOLARRADIATION if key == TYPE_SOLARRADIATION_LX else key - self._attr_available = last_data[available_key] is not None - self.update_from_latest_data() - self.async_write_ha_state() - - async def async_added_to_hass(self) -> None: - """Register callbacks.""" - self.async_on_remove( - async_dispatcher_connect( - self.hass, - f"ambient_station_data_update_{self._mac_address}", - self._async_update, - ) - ) - - self.update_from_latest_data() - - @callback - def update_from_latest_data(self) -> None: - """Update the entity from the latest data.""" - raise NotImplementedError diff --git a/homeassistant/components/ambient_station/binary_sensor.py b/homeassistant/components/ambient_station/binary_sensor.py index a58a0ec6f85..49ff43bcc7e 100644 --- a/homeassistant/components/ambient_station/binary_sensor.py +++ b/homeassistant/components/ambient_station/binary_sensor.py @@ -14,8 +14,8 @@ from homeassistant.const import ATTR_NAME, EntityCategory from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AmbientWeatherEntity from .const import ATTR_LAST_DATA, DOMAIN +from .entity import AmbientWeatherEntity TYPE_BATT1 = "batt1" TYPE_BATT10 = "batt10" diff --git a/homeassistant/components/ambient_station/entity.py b/homeassistant/components/ambient_station/entity.py new file mode 100644 index 00000000000..277b69e8f68 --- /dev/null +++ b/homeassistant/components/ambient_station/entity.py @@ -0,0 +1,70 @@ +"""Base entity Ambient Weather Station Service.""" +from __future__ import annotations + +from aioambient.util import get_public_device_id + +from homeassistant.core import callback +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import Entity, EntityDescription + +from . import AmbientStation +from .const import ATTR_LAST_DATA, DOMAIN, TYPE_SOLARRADIATION, TYPE_SOLARRADIATION_LX + + +class AmbientWeatherEntity(Entity): + """Define a base Ambient PWS entity.""" + + _attr_has_entity_name = True + _attr_should_poll = False + + def __init__( + self, + ambient: AmbientStation, + mac_address: str, + station_name: str, + description: EntityDescription, + ) -> None: + """Initialize the entity.""" + self._ambient = ambient + + public_device_id = get_public_device_id(mac_address) + self._attr_device_info = DeviceInfo( + configuration_url=( + f"https://ambientweather.net/dashboard/{public_device_id}" + ), + identifiers={(DOMAIN, mac_address)}, + manufacturer="Ambient Weather", + name=station_name.capitalize(), + ) + + self._attr_unique_id = f"{mac_address}_{description.key}" + self._mac_address = mac_address + self.entity_description = description + + @callback + def _async_update(self) -> None: + """Update the state.""" + last_data = self._ambient.stations[self._mac_address][ATTR_LAST_DATA] + key = self.entity_description.key + available_key = TYPE_SOLARRADIATION if key == TYPE_SOLARRADIATION_LX else key + self._attr_available = last_data[available_key] is not None + self.update_from_latest_data() + self.async_write_ha_state() + + async def async_added_to_hass(self) -> None: + """Register callbacks.""" + self.async_on_remove( + async_dispatcher_connect( + self.hass, + f"ambient_station_data_update_{self._mac_address}", + self._async_update, + ) + ) + + self.update_from_latest_data() + + @callback + def update_from_latest_data(self) -> None: + """Update the entity from the latest data.""" + raise NotImplementedError diff --git a/homeassistant/components/ambient_station/sensor.py b/homeassistant/components/ambient_station/sensor.py index e1f624da52f..4873da566b5 100644 --- a/homeassistant/components/ambient_station/sensor.py +++ b/homeassistant/components/ambient_station/sensor.py @@ -28,8 +28,9 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AmbientStation, AmbientWeatherEntity +from . import AmbientStation from .const import ATTR_LAST_DATA, DOMAIN, TYPE_SOLARRADIATION, TYPE_SOLARRADIATION_LX +from .entity import AmbientWeatherEntity TYPE_24HOURRAININ = "24hourrainin" TYPE_AQI_PM25 = "aqi_pm25"