1 """Support for Efergy sensors."""
3 from __future__
import annotations
7 from typing
import cast
9 from pyefergy
import Efergy
10 from pyefergy.exceptions
import ConnectError, DataError, ServiceError
15 SensorEntityDescription,
23 from .
import EfergyConfigEntry
24 from .const
import CONF_CURRENT_VALUES, LOGGER
25 from .entity
import EfergyEntity
27 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
29 key=
"instant_readings",
30 translation_key=
"instant_readings",
31 device_class=SensorDeviceClass.POWER,
32 native_unit_of_measurement=UnitOfPower.WATT,
33 state_class=SensorStateClass.MEASUREMENT,
37 translation_key=
"energy_day",
38 device_class=SensorDeviceClass.ENERGY,
39 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
40 state_class=SensorStateClass.TOTAL_INCREASING,
41 entity_registry_enabled_default=
False,
45 translation_key=
"energy_week",
46 device_class=SensorDeviceClass.ENERGY,
47 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
48 state_class=SensorStateClass.TOTAL_INCREASING,
49 entity_registry_enabled_default=
False,
53 translation_key=
"energy_month",
54 device_class=SensorDeviceClass.ENERGY,
55 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
56 state_class=SensorStateClass.TOTAL_INCREASING,
60 translation_key=
"energy_year",
61 device_class=SensorDeviceClass.ENERGY,
62 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
63 state_class=SensorStateClass.TOTAL_INCREASING,
64 entity_registry_enabled_default=
False,
68 translation_key=
"budget",
69 entity_registry_enabled_default=
False,
73 translation_key=
"cost_day",
74 device_class=SensorDeviceClass.MONETARY,
75 state_class=SensorStateClass.TOTAL_INCREASING,
76 entity_registry_enabled_default=
False,
80 translation_key=
"cost_week",
81 device_class=SensorDeviceClass.MONETARY,
82 state_class=SensorStateClass.TOTAL_INCREASING,
83 entity_registry_enabled_default=
False,
87 translation_key=
"cost_month",
88 device_class=SensorDeviceClass.MONETARY,
89 state_class=SensorStateClass.TOTAL_INCREASING,
93 translation_key=
"cost_year",
94 device_class=SensorDeviceClass.MONETARY,
95 state_class=SensorStateClass.TOTAL_INCREASING,
96 entity_registry_enabled_default=
False,
99 key=CONF_CURRENT_VALUES,
100 translation_key=
"power_usage",
101 device_class=SensorDeviceClass.POWER,
102 native_unit_of_measurement=UnitOfPower.WATT,
103 state_class=SensorStateClass.MEASUREMENT,
110 entry: EfergyConfigEntry,
111 async_add_entities: AddEntitiesCallback,
113 """Set up Efergy sensors."""
114 api = entry.runtime_data
116 for description
in SENSOR_TYPES:
117 if description.key != CONF_CURRENT_VALUES:
123 period=sub(
"^energy_|^cost_",
"", description.key),
124 currency=hass.config.currency,
128 description = dataclasses.replace(
130 entity_registry_enabled_default=len(api.sids) > 1,
145 """Implementation of an Efergy sensor."""
147 _attr_has_entity_name =
True
152 description: SensorEntityDescription,
153 server_unique_id: str,
154 period: str |
None =
None,
155 currency: str |
None =
None,
156 sid: int |
None =
None,
158 """Initialize the sensor."""
159 super().
__init__(api, server_unique_id)
161 if description.key == CONF_CURRENT_VALUES:
162 assert sid
is not None
165 f
"{server_unique_id}/{description.key}_{'' if sid is None else sid}"
167 if "cost" in description.key:
173 """Get the Efergy monitor data from the web service."""
175 data = await self.
apiapi.async_get_reading(
179 except (ConnectError, DataError, ServiceError)
as ex:
182 LOGGER.error(
"Error getting data: %s", ex)
186 LOGGER.debug(
"Connection has resumed")
_attr_native_unit_of_measurement
None __init__(self, Efergy api, SensorEntityDescription description, str server_unique_id, str|None period=None, str|None currency=None, int|None sid=None)
_attr_translation_placeholders
None async_setup_entry(HomeAssistant hass, EfergyConfigEntry entry, AddEntitiesCallback async_add_entities)