mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +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/flock/notify.py
|
||||||
homeassistant/components/flume/__init__.py
|
homeassistant/components/flume/__init__.py
|
||||||
homeassistant/components/flume/coordinator.py
|
homeassistant/components/flume/coordinator.py
|
||||||
|
homeassistant/components/flume/entity.py
|
||||||
homeassistant/components/flume/sensor.py
|
homeassistant/components/flume/sensor.py
|
||||||
homeassistant/components/flunearyou/__init__.py
|
homeassistant/components/flunearyou/__init__.py
|
||||||
homeassistant/components/flunearyou/repairs.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.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_NAME
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DEFAULT_NAME,
|
|
||||||
DEVICE_SCAN_INTERVAL,
|
DEVICE_SCAN_INTERVAL,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
FLUME_AUTH,
|
FLUME_AUTH,
|
||||||
@ -22,11 +18,11 @@ from .const import (
|
|||||||
FLUME_TYPE_SENSOR,
|
FLUME_TYPE_SENSOR,
|
||||||
KEY_DEVICE_ID,
|
KEY_DEVICE_ID,
|
||||||
KEY_DEVICE_LOCATION,
|
KEY_DEVICE_LOCATION,
|
||||||
KEY_DEVICE_LOCATION_NAME,
|
|
||||||
KEY_DEVICE_LOCATION_TIMEZONE,
|
KEY_DEVICE_LOCATION_TIMEZONE,
|
||||||
KEY_DEVICE_TYPE,
|
KEY_DEVICE_TYPE,
|
||||||
)
|
)
|
||||||
from .coordinator import FlumeDeviceDataUpdateCoordinator
|
from .coordinator import FlumeDeviceDataUpdateCoordinator
|
||||||
|
from .entity import FlumeEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -35,24 +31,20 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Flume sensor."""
|
"""Set up the Flume sensor."""
|
||||||
|
|
||||||
flume_domain_data = hass.data[DOMAIN][config_entry.entry_id]
|
flume_domain_data = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
flume_auth = flume_domain_data[FLUME_AUTH]
|
flume_auth = flume_domain_data[FLUME_AUTH]
|
||||||
http_session = flume_domain_data[FLUME_HTTP_SESSION]
|
http_session = flume_domain_data[FLUME_HTTP_SESSION]
|
||||||
flume_devices = flume_domain_data[FLUME_DEVICES]
|
flume_devices = flume_domain_data[FLUME_DEVICES]
|
||||||
|
|
||||||
config = config_entry.data
|
|
||||||
name = config.get(CONF_NAME, DEFAULT_NAME)
|
|
||||||
|
|
||||||
flume_entity_list = []
|
flume_entity_list = []
|
||||||
for device in flume_devices.device_list:
|
for device in flume_devices.device_list:
|
||||||
if device[KEY_DEVICE_TYPE] != FLUME_TYPE_SENSOR:
|
if device[KEY_DEVICE_TYPE] != FLUME_TYPE_SENSOR:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
device_id = device[KEY_DEVICE_ID]
|
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_timezone = device[KEY_DEVICE_LOCATION][KEY_DEVICE_LOCATION_TIMEZONE]
|
||||||
device_friendly_name = f"{name} {device_name}"
|
|
||||||
|
|
||||||
flume_device = FlumeData(
|
flume_device = FlumeData(
|
||||||
flume_auth,
|
flume_auth,
|
||||||
@ -72,7 +64,6 @@ async def async_setup_entry(
|
|||||||
FlumeSensor(
|
FlumeSensor(
|
||||||
coordinator=coordinator,
|
coordinator=coordinator,
|
||||||
description=description,
|
description=description,
|
||||||
name=device_friendly_name,
|
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
)
|
)
|
||||||
for description in FLUME_QUERIES_SENSOR
|
for description in FLUME_QUERIES_SENSOR
|
||||||
@ -83,28 +74,19 @@ async def async_setup_entry(
|
|||||||
async_add_entities(flume_entity_list)
|
async_add_entities(flume_entity_list)
|
||||||
|
|
||||||
|
|
||||||
class FlumeSensor(CoordinatorEntity, SensorEntity):
|
class FlumeSensor(FlumeEntity, SensorEntity):
|
||||||
"""Representation of the Flume sensor."""
|
"""Representation of the Flume sensor."""
|
||||||
|
|
||||||
|
coordinator: FlumeDeviceDataUpdateCoordinator
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator,
|
coordinator: FlumeDeviceDataUpdateCoordinator,
|
||||||
name,
|
device_id: str,
|
||||||
device_id,
|
|
||||||
description: SensorEntityDescription,
|
description: SensorEntityDescription,
|
||||||
):
|
) -> None:
|
||||||
"""Initialize the Flume sensor."""
|
"""Inlitializer function with type hints."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator, description, device_id)
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
@ -115,13 +97,6 @@ class FlumeSensor(CoordinatorEntity, SensorEntity):
|
|||||||
|
|
||||||
return _format_state_value(self.coordinator.flume_device.values[sensor_key])
|
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):
|
def _format_state_value(value):
|
||||||
return round(value, 1) if isinstance(value, Number) else None
|
return round(value, 1) if isinstance(value, Number) else None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user