Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for V2C EVSE sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import logging
8 
9 from pytrydan import TrydanData
10 from pytrydan.models.trydan import SlaveCommunicationState
11 
13  SensorDeviceClass,
14  SensorEntity,
15  SensorEntityDescription,
16  SensorStateClass,
17 )
18 from homeassistant.const import (
19  EntityCategory,
20  UnitOfElectricPotential,
21  UnitOfEnergy,
22  UnitOfPower,
23  UnitOfTime,
24 )
25 from homeassistant.core import HomeAssistant
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 from homeassistant.helpers.typing import StateType
28 
29 from . import V2CConfigEntry
30 from .coordinator import V2CUpdateCoordinator
31 from .entity import V2CBaseEntity
32 
33 _LOGGER = logging.getLogger(__name__)
34 
35 
36 @dataclass(frozen=True, kw_only=True)
38  """Describes an EVSE Power sensor entity."""
39 
40  value_fn: Callable[[TrydanData], StateType]
41 
42 
43 def get_meter_value(value: SlaveCommunicationState) -> str:
44  """Return the value of the enum and replace slave by meter."""
45  return value.name.lower().replace("slave", "meter")
46 
47 
48 _METER_ERROR_OPTIONS = [get_meter_value(error) for error in SlaveCommunicationState]
49 
50 TRYDAN_SENSORS = (
52  key="charge_power",
53  translation_key="charge_power",
54  native_unit_of_measurement=UnitOfPower.WATT,
55  state_class=SensorStateClass.MEASUREMENT,
56  device_class=SensorDeviceClass.POWER,
57  value_fn=lambda evse_data: evse_data.charge_power,
58  ),
60  key="voltage_installation",
61  translation_key="voltage_installation",
62  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
63  state_class=SensorStateClass.MEASUREMENT,
64  device_class=SensorDeviceClass.VOLTAGE,
65  value_fn=lambda evse_data: evse_data.voltage_installation,
66  entity_registry_enabled_default=False,
67  ),
69  key="charge_energy",
70  translation_key="charge_energy",
71  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
72  state_class=SensorStateClass.TOTAL_INCREASING,
73  device_class=SensorDeviceClass.ENERGY,
74  value_fn=lambda evse_data: evse_data.charge_energy,
75  ),
77  key="charge_time",
78  translation_key="charge_time",
79  native_unit_of_measurement=UnitOfTime.SECONDS,
80  state_class=SensorStateClass.TOTAL_INCREASING,
81  device_class=SensorDeviceClass.DURATION,
82  value_fn=lambda evse_data: evse_data.charge_time,
83  ),
85  key="house_power",
86  translation_key="house_power",
87  native_unit_of_measurement=UnitOfPower.WATT,
88  state_class=SensorStateClass.MEASUREMENT,
89  device_class=SensorDeviceClass.POWER,
90  value_fn=lambda evse_data: evse_data.house_power,
91  ),
93  key="fv_power",
94  translation_key="fv_power",
95  native_unit_of_measurement=UnitOfPower.WATT,
96  state_class=SensorStateClass.MEASUREMENT,
97  device_class=SensorDeviceClass.POWER,
98  value_fn=lambda evse_data: evse_data.fv_power,
99  ),
101  key="meter_error",
102  translation_key="meter_error",
103  entity_category=EntityCategory.DIAGNOSTIC,
104  value_fn=lambda evse_data: get_meter_value(evse_data.slave_error),
105  entity_registry_enabled_default=False,
106  device_class=SensorDeviceClass.ENUM,
107  options=_METER_ERROR_OPTIONS,
108  ),
110  key="battery_power",
111  translation_key="battery_power",
112  native_unit_of_measurement=UnitOfPower.WATT,
113  state_class=SensorStateClass.MEASUREMENT,
114  device_class=SensorDeviceClass.POWER,
115  value_fn=lambda evse_data: evse_data.battery_power,
116  entity_registry_enabled_default=False,
117  ),
119  key="ssid",
120  translation_key="ssid",
121  entity_category=EntityCategory.DIAGNOSTIC,
122  value_fn=lambda evse_data: evse_data.SSID,
123  entity_registry_enabled_default=False,
124  ),
126  key="ip_address",
127  translation_key="ip_address",
128  entity_category=EntityCategory.DIAGNOSTIC,
129  value_fn=lambda evse_data: evse_data.IP,
130  entity_registry_enabled_default=False,
131  ),
133  key="signal_status",
134  translation_key="signal_status",
135  entity_category=EntityCategory.DIAGNOSTIC,
136  state_class=SensorStateClass.MEASUREMENT,
137  value_fn=lambda evse_data: evse_data.signal_status,
138  entity_registry_enabled_default=False,
139  ),
140 )
141 
142 
144  hass: HomeAssistant,
145  config_entry: V2CConfigEntry,
146  async_add_entities: AddEntitiesCallback,
147 ) -> None:
148  """Set up V2C sensor platform."""
149  coordinator = config_entry.runtime_data
150 
152  V2CSensorBaseEntity(coordinator, description, config_entry.entry_id)
153  for description in TRYDAN_SENSORS
154  )
155 
156 
158  """Defines a base v2c sensor entity."""
159 
160  entity_description: V2CSensorEntityDescription
161 
162  def __init__(
163  self,
164  coordinator: V2CUpdateCoordinator,
165  description: SensorEntityDescription,
166  entry_id: str,
167  ) -> None:
168  """Initialize V2C Power entity."""
169  super().__init__(coordinator, description)
170  self._attr_unique_id_attr_unique_id = f"{entry_id}_{description.key}"
171 
172  @property
173  def native_value(self) -> StateType:
174  """Return the state of the sensor."""
175  return self.entity_descriptionentity_description.value_fn(self.datadatadata)
None __init__(self, V2CUpdateCoordinator coordinator, SensorEntityDescription description, str entry_id)
Definition: sensor.py:167
None async_setup_entry(HomeAssistant hass, V2CConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:147
str get_meter_value(SlaveCommunicationState value)
Definition: sensor.py:43