Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Streamlabs Water Monitor Usage."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import UnitOfVolume
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.typing import StateType
18 
19 from . import StreamlabsCoordinator
20 from .const import DOMAIN
21 from .coordinator import StreamlabsData
22 from .entity import StreamlabsWaterEntity
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  """Streamlabs sensor entity description."""
28 
29  value_fn: Callable[[StreamlabsData], StateType]
30 
31 
32 SENSORS: tuple[StreamlabsWaterSensorEntityDescription, ...] = (
34  key="daily_usage",
35  translation_key="daily_usage",
36  native_unit_of_measurement=UnitOfVolume.GALLONS,
37  device_class=SensorDeviceClass.WATER,
38  suggested_display_precision=1,
39  value_fn=lambda data: data.daily_usage,
40  ),
42  key="monthly_usage",
43  translation_key="monthly_usage",
44  native_unit_of_measurement=UnitOfVolume.GALLONS,
45  device_class=SensorDeviceClass.WATER,
46  suggested_display_precision=1,
47  value_fn=lambda data: data.monthly_usage,
48  ),
50  key="yearly_usage",
51  translation_key="yearly_usage",
52  native_unit_of_measurement=UnitOfVolume.GALLONS,
53  device_class=SensorDeviceClass.WATER,
54  suggested_display_precision=1,
55  value_fn=lambda data: data.yearly_usage,
56  ),
57 )
58 
59 
61  hass: HomeAssistant,
62  entry: ConfigEntry,
63  async_add_entities: AddEntitiesCallback,
64 ) -> None:
65  """Set up Streamlabs water sensor from a config entry."""
66  coordinator = hass.data[DOMAIN][entry.entry_id]
67 
69  StreamLabsSensor(coordinator, location_id, entity_description)
70  for location_id in coordinator.data
71  for entity_description in SENSORS
72  )
73 
74 
76  """Monitors the daily water usage."""
77 
78  entity_description: StreamlabsWaterSensorEntityDescription
79 
80  def __init__(
81  self,
82  coordinator: StreamlabsCoordinator,
83  location_id: str,
84  entity_description: StreamlabsWaterSensorEntityDescription,
85  ) -> None:
86  """Initialize the daily water usage device."""
87  super().__init__(coordinator, location_id, entity_description.key)
88  self.entity_descriptionentity_description = entity_description
89 
90  @property
91  def native_value(self) -> StateType:
92  """Return the current daily usage."""
93  return self.entity_descriptionentity_description.value_fn(self.location_datalocation_data)
None __init__(self, StreamlabsCoordinator coordinator, str location_id, StreamlabsWaterSensorEntityDescription entity_description)
Definition: sensor.py:85
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:64