Extract Ambient Station base entity to separate file (#99142)

* Extract Ambient Station entity to separate file

* Add to coveragerc
This commit is contained in:
Joost Lekkerkerker 2023-08-27 20:09:10 +02:00 committed by GitHub
parent 65103d4515
commit fbe2228c3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 67 deletions

View file

@ -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/*

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -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"