Add entity descriptions to Streamlabs water (#107071)

* Add sensor tests to Streamlabs water

* Add sensor tests to Streamlabs water

* Use entity descriptions in streamlabs water

* Use entity descriptions in streamlabs water

* Use entity descriptions in streamlabs water

* Add translations
This commit is contained in:
Joost Lekkerkerker 2024-01-04 11:53:04 +01:00 committed by GitHub
parent 5508bb3ef9
commit 5ae8b6bc02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 85 deletions

View File

@ -1,20 +1,57 @@
"""Support for Streamlabs Water Monitor Usage."""
from __future__ import annotations
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfVolume
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import StreamlabsCoordinator
from .const import DOMAIN
from .coordinator import StreamlabsData
NAME_DAILY_USAGE = "Daily Water"
NAME_MONTHLY_USAGE = "Monthly Water"
NAME_YEARLY_USAGE = "Yearly Water"
@dataclass(frozen=True, kw_only=True)
class StreamlabsWaterSensorEntityDescription(SensorEntityDescription):
"""Streamlabs sensor entity description."""
value_fn: Callable[[StreamlabsData], StateType]
SENSORS: tuple[StreamlabsWaterSensorEntityDescription, ...] = (
StreamlabsWaterSensorEntityDescription(
key="daily_usage",
translation_key="daily_usage",
native_unit_of_measurement=UnitOfVolume.GALLONS,
device_class=SensorDeviceClass.WATER,
value_fn=lambda data: data.daily_usage,
),
StreamlabsWaterSensorEntityDescription(
key="monthly_usage",
translation_key="monthly_usage",
native_unit_of_measurement=UnitOfVolume.GALLONS,
device_class=SensorDeviceClass.WATER,
value_fn=lambda data: data.monthly_usage,
),
StreamlabsWaterSensorEntityDescription(
key="yearly_usage",
translation_key="yearly_usage",
native_unit_of_measurement=UnitOfVolume.GALLONS,
device_class=SensorDeviceClass.WATER,
value_fn=lambda data: data.yearly_usage,
),
)
async def async_setup_entry(
@ -25,32 +62,34 @@ async def async_setup_entry(
"""Set up Streamlabs water sensor from a config entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
entities = []
for location_id in coordinator.data:
entities.extend(
[
StreamLabsDailyUsage(coordinator, location_id),
StreamLabsMonthlyUsage(coordinator, location_id),
StreamLabsYearlyUsage(coordinator, location_id),
]
)
async_add_entities(entities)
async_add_entities(
StreamLabsSensor(coordinator, location_id, entity_description)
for location_id in coordinator.data
for entity_description in SENSORS
)
class StreamLabsDailyUsage(CoordinatorEntity[StreamlabsCoordinator], SensorEntity):
class StreamLabsSensor(CoordinatorEntity[StreamlabsCoordinator], SensorEntity):
"""Monitors the daily water usage."""
_attr_device_class = SensorDeviceClass.WATER
_attr_native_unit_of_measurement = UnitOfVolume.GALLONS
_key = "daily_usage"
_attr_has_entity_name = True
def __init__(self, coordinator: StreamlabsCoordinator, location_id: str) -> None:
entity_description: StreamlabsWaterSensorEntityDescription
def __init__(
self,
coordinator: StreamlabsCoordinator,
location_id: str,
entity_description: StreamlabsWaterSensorEntityDescription,
) -> None:
"""Initialize the daily water usage device."""
super().__init__(coordinator)
self._location_id = location_id
self._attr_unique_id = f"{location_id}-{self._key}"
self._attr_unique_id = f"{location_id}-{entity_description.key}"
self.entity_description = entity_description
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, location_id)}, name=self.location_data.name
)
@property
def location_data(self) -> StreamlabsData:
@ -58,43 +97,6 @@ class StreamLabsDailyUsage(CoordinatorEntity[StreamlabsCoordinator], SensorEntit
return self.coordinator.data[self._location_id]
@property
def name(self) -> str:
"""Return the name for daily usage."""
return f"{self.location_data.name} {NAME_DAILY_USAGE}"
@property
def native_value(self) -> float:
def native_value(self) -> StateType:
"""Return the current daily usage."""
return self.location_data.daily_usage
class StreamLabsMonthlyUsage(StreamLabsDailyUsage):
"""Monitors the monthly water usage."""
_key = "monthly_usage"
@property
def name(self) -> str:
"""Return the name for monthly usage."""
return f"{self.location_data.name} {NAME_MONTHLY_USAGE}"
@property
def native_value(self) -> float:
"""Return the current monthly usage."""
return self.location_data.monthly_usage
class StreamLabsYearlyUsage(StreamLabsDailyUsage):
"""Monitors the yearly water usage."""
_key = "yearly_usage"
@property
def name(self) -> str:
"""Return the name for yearly usage."""
return f"{self.location_data.name} {NAME_YEARLY_USAGE}"
@property
def native_value(self) -> float:
"""Return the current yearly usage."""
return self.location_data.yearly_usage
return self.entity_description.value_fn(self.location_data)

View File

@ -31,6 +31,19 @@
}
}
},
"entity": {
"sensor": {
"daily_usage": {
"name": "Daily usage"
},
"monthly_usage": {
"name": "Monthly usage"
},
"yearly_usage": {
"name": "Yearly usage"
}
}
},
"issues": {
"deprecated_yaml_import_issue_cannot_connect": {
"title": "The Streamlabs water YAML configuration import failed",

View File

@ -1,5 +1,5 @@
# serializer version: 1
# name: test_all_entities[sensor.water_monitor_daily_water-entry]
# name: test_all_entities[sensor.water_monitor_daily_usage-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
@ -11,8 +11,8 @@
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.water_monitor_daily_water',
'has_entity_name': False,
'entity_id': 'sensor.water_monitor_daily_usage',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
@ -21,30 +21,30 @@
}),
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
'original_icon': None,
'original_name': 'Water Monitor Daily Water',
'original_name': 'Daily usage',
'platform': 'streamlabswater',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'translation_key': 'daily_usage',
'unique_id': '945e7c52-854a-41e1-8524-50c6993277e1-daily_usage',
'unit_of_measurement': <UnitOfVolume.GALLONS: 'gal'>,
})
# ---
# name: test_all_entities[sensor.water_monitor_daily_water-state]
# name: test_all_entities[sensor.water_monitor_daily_usage-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'water',
'friendly_name': 'Water Monitor Daily Water',
'friendly_name': 'Water Monitor Daily usage',
'unit_of_measurement': <UnitOfVolume.GALLONS: 'gal'>,
}),
'context': <ANY>,
'entity_id': 'sensor.water_monitor_daily_water',
'entity_id': 'sensor.water_monitor_daily_usage',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '200.4',
})
# ---
# name: test_all_entities[sensor.water_monitor_monthly_water-entry]
# name: test_all_entities[sensor.water_monitor_monthly_usage-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
@ -56,8 +56,8 @@
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.water_monitor_monthly_water',
'has_entity_name': False,
'entity_id': 'sensor.water_monitor_monthly_usage',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
@ -66,30 +66,30 @@
}),
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
'original_icon': None,
'original_name': 'Water Monitor Monthly Water',
'original_name': 'Monthly usage',
'platform': 'streamlabswater',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'translation_key': 'monthly_usage',
'unique_id': '945e7c52-854a-41e1-8524-50c6993277e1-monthly_usage',
'unit_of_measurement': <UnitOfVolume.GALLONS: 'gal'>,
})
# ---
# name: test_all_entities[sensor.water_monitor_monthly_water-state]
# name: test_all_entities[sensor.water_monitor_monthly_usage-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'water',
'friendly_name': 'Water Monitor Monthly Water',
'friendly_name': 'Water Monitor Monthly usage',
'unit_of_measurement': <UnitOfVolume.GALLONS: 'gal'>,
}),
'context': <ANY>,
'entity_id': 'sensor.water_monitor_monthly_water',
'entity_id': 'sensor.water_monitor_monthly_usage',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '420.5',
})
# ---
# name: test_all_entities[sensor.water_monitor_yearly_water-entry]
# name: test_all_entities[sensor.water_monitor_yearly_usage-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
@ -101,8 +101,8 @@
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.water_monitor_yearly_water',
'has_entity_name': False,
'entity_id': 'sensor.water_monitor_yearly_usage',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
@ -111,24 +111,24 @@
}),
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
'original_icon': None,
'original_name': 'Water Monitor Yearly Water',
'original_name': 'Yearly usage',
'platform': 'streamlabswater',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'translation_key': 'yearly_usage',
'unique_id': '945e7c52-854a-41e1-8524-50c6993277e1-yearly_usage',
'unit_of_measurement': <UnitOfVolume.GALLONS: 'gal'>,
})
# ---
# name: test_all_entities[sensor.water_monitor_yearly_water-state]
# name: test_all_entities[sensor.water_monitor_yearly_usage-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'water',
'friendly_name': 'Water Monitor Yearly Water',
'friendly_name': 'Water Monitor Yearly usage',
'unit_of_measurement': <UnitOfVolume.GALLONS: 'gal'>,
}),
'context': <ANY>,
'entity_id': 'sensor.water_monitor_yearly_water',
'entity_id': 'sensor.water_monitor_yearly_usage',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '65432.4',