mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Refactor Flume to use base entity class (#77115)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
8c24d5810c
commit
edb4e4de35
@ -394,6 +394,7 @@ omit =
|
||||
homeassistant/components/flock/notify.py
|
||||
homeassistant/components/flume/__init__.py
|
||||
homeassistant/components/flume/coordinator.py
|
||||
homeassistant/components/flume/entity.py
|
||||
homeassistant/components/flume/sensor.py
|
||||
homeassistant/components/flunearyou/__init__.py
|
||||
homeassistant/components/flunearyou/repairs.py
|
||||
|
41
homeassistant/components/flume/entity.py
Normal file
41
homeassistant/components/flume/entity.py
Normal file
@ -0,0 +1,41 @@
|
||||
"""Platform for shared base classes for sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import FlumeDeviceDataUpdateCoordinator
|
||||
|
||||
|
||||
class FlumeEntity(CoordinatorEntity[FlumeDeviceDataUpdateCoordinator]):
|
||||
"""Base entity class."""
|
||||
|
||||
_attr_attribution = "Data provided by Flume API"
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: FlumeDeviceDataUpdateCoordinator,
|
||||
description: EntityDescription,
|
||||
device_id: str,
|
||||
) -> None:
|
||||
"""Class initializer."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
self.device_id = device_id
|
||||
self._attr_unique_id = f"{description.key}_{device_id}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, device_id)},
|
||||
manufacturer="Flume, Inc.",
|
||||
model="Flume Smart Water Monitor",
|
||||
name=f"Flume {device_id}",
|
||||
configuration_url="https://portal.flumewater.com",
|
||||
)
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Request an update when added."""
|
||||
await super().async_added_to_hass()
|
||||
# We do not ask for an update with async_add_entities()
|
||||
# because it will update disabled entities
|
||||
await self.coordinator.async_request_refresh()
|
@ -5,14 +5,10 @@ from pyflume import FlumeData
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import (
|
||||
DEFAULT_NAME,
|
||||
DEVICE_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
FLUME_AUTH,
|
||||
@ -22,11 +18,11 @@ from .const import (
|
||||
FLUME_TYPE_SENSOR,
|
||||
KEY_DEVICE_ID,
|
||||
KEY_DEVICE_LOCATION,
|
||||
KEY_DEVICE_LOCATION_NAME,
|
||||
KEY_DEVICE_LOCATION_TIMEZONE,
|
||||
KEY_DEVICE_TYPE,
|
||||
)
|
||||
from .coordinator import FlumeDeviceDataUpdateCoordinator
|
||||
from .entity import FlumeEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
@ -35,24 +31,20 @@ async def async_setup_entry(
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Flume sensor."""
|
||||
|
||||
flume_domain_data = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
flume_auth = flume_domain_data[FLUME_AUTH]
|
||||
http_session = flume_domain_data[FLUME_HTTP_SESSION]
|
||||
flume_devices = flume_domain_data[FLUME_DEVICES]
|
||||
|
||||
config = config_entry.data
|
||||
name = config.get(CONF_NAME, DEFAULT_NAME)
|
||||
|
||||
flume_entity_list = []
|
||||
for device in flume_devices.device_list:
|
||||
if device[KEY_DEVICE_TYPE] != FLUME_TYPE_SENSOR:
|
||||
continue
|
||||
|
||||
device_id = device[KEY_DEVICE_ID]
|
||||
device_name = device[KEY_DEVICE_LOCATION][KEY_DEVICE_LOCATION_NAME]
|
||||
device_timezone = device[KEY_DEVICE_LOCATION][KEY_DEVICE_LOCATION_TIMEZONE]
|
||||
device_friendly_name = f"{name} {device_name}"
|
||||
|
||||
flume_device = FlumeData(
|
||||
flume_auth,
|
||||
@ -72,7 +64,6 @@ async def async_setup_entry(
|
||||
FlumeSensor(
|
||||
coordinator=coordinator,
|
||||
description=description,
|
||||
name=device_friendly_name,
|
||||
device_id=device_id,
|
||||
)
|
||||
for description in FLUME_QUERIES_SENSOR
|
||||
@ -83,28 +74,19 @@ async def async_setup_entry(
|
||||
async_add_entities(flume_entity_list)
|
||||
|
||||
|
||||
class FlumeSensor(CoordinatorEntity, SensorEntity):
|
||||
class FlumeSensor(FlumeEntity, SensorEntity):
|
||||
"""Representation of the Flume sensor."""
|
||||
|
||||
coordinator: FlumeDeviceDataUpdateCoordinator
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator,
|
||||
name,
|
||||
device_id,
|
||||
coordinator: FlumeDeviceDataUpdateCoordinator,
|
||||
device_id: str,
|
||||
description: SensorEntityDescription,
|
||||
):
|
||||
"""Initialize the Flume sensor."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
|
||||
self._attr_name = f"{name} {description.name}"
|
||||
self._attr_unique_id = f"{description.key}_{device_id}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, device_id)},
|
||||
manufacturer="Flume, Inc.",
|
||||
model="Flume Smart Water Monitor",
|
||||
name=self.name,
|
||||
)
|
||||
) -> None:
|
||||
"""Inlitializer function with type hints."""
|
||||
super().__init__(coordinator, description, device_id)
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
@ -115,13 +97,6 @@ class FlumeSensor(CoordinatorEntity, SensorEntity):
|
||||
|
||||
return _format_state_value(self.coordinator.flume_device.values[sensor_key])
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Request an update when added."""
|
||||
await super().async_added_to_hass()
|
||||
# We do not ask for an update with async_add_entities()
|
||||
# because it will update disabled entities
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
|
||||
def _format_state_value(value):
|
||||
return round(value, 1) if isinstance(value, Number) else None
|
||||
|
Loading…
x
Reference in New Issue
Block a user