1 """Support for Opower sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
8 from opower
import Forecast, MeterType, UnitOfMeasure
13 SensorEntityDescription,
24 from .const
import DOMAIN
25 from .coordinator
import OpowerCoordinator
28 @dataclass(frozen=True, kw_only=True)
30 """Class describing Opower sensors entities."""
32 value_fn: Callable[[Forecast], str | float]
38 ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
40 key=
"elec_usage_to_date",
41 name=
"Current bill electric usage to date",
42 device_class=SensorDeviceClass.ENERGY,
43 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
45 state_class=SensorStateClass.TOTAL,
46 suggested_display_precision=0,
47 value_fn=
lambda data: data.usage_to_date,
50 key=
"elec_forecasted_usage",
51 name=
"Current bill electric forecasted usage",
52 device_class=SensorDeviceClass.ENERGY,
53 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
54 state_class=SensorStateClass.TOTAL,
55 suggested_display_precision=0,
56 value_fn=
lambda data: data.forecasted_usage,
59 key=
"elec_typical_usage",
60 name=
"Typical monthly electric usage",
61 device_class=SensorDeviceClass.ENERGY,
62 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
63 state_class=SensorStateClass.TOTAL,
64 suggested_display_precision=0,
65 value_fn=
lambda data: data.typical_usage,
68 key=
"elec_cost_to_date",
69 name=
"Current bill electric cost to date",
70 device_class=SensorDeviceClass.MONETARY,
71 native_unit_of_measurement=
"USD",
72 state_class=SensorStateClass.TOTAL,
73 suggested_display_precision=0,
74 value_fn=
lambda data: data.cost_to_date,
77 key=
"elec_forecasted_cost",
78 name=
"Current bill electric forecasted cost",
79 device_class=SensorDeviceClass.MONETARY,
80 native_unit_of_measurement=
"USD",
81 state_class=SensorStateClass.TOTAL,
82 suggested_display_precision=0,
83 value_fn=
lambda data: data.forecasted_cost,
86 key=
"elec_typical_cost",
87 name=
"Typical monthly electric cost",
88 device_class=SensorDeviceClass.MONETARY,
89 native_unit_of_measurement=
"USD",
90 state_class=SensorStateClass.TOTAL,
91 suggested_display_precision=0,
92 value_fn=
lambda data: data.typical_cost,
95 key=
"elec_start_date",
96 name=
"Current bill electric start date",
97 device_class=SensorDeviceClass.DATE,
98 entity_category=EntityCategory.DIAGNOSTIC,
99 entity_registry_enabled_default=
False,
100 value_fn=
lambda data: data.start_date,
104 name=
"Current bill electric end date",
105 device_class=SensorDeviceClass.DATE,
106 entity_category=EntityCategory.DIAGNOSTIC,
107 entity_registry_enabled_default=
False,
108 value_fn=
lambda data: data.end_date,
111 GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
113 key=
"gas_usage_to_date",
114 name=
"Current bill gas usage to date",
115 device_class=SensorDeviceClass.GAS,
116 native_unit_of_measurement=UnitOfVolume.CENTUM_CUBIC_FEET,
117 state_class=SensorStateClass.TOTAL,
118 suggested_display_precision=0,
119 value_fn=
lambda data: data.usage_to_date,
122 key=
"gas_forecasted_usage",
123 name=
"Current bill gas forecasted usage",
124 device_class=SensorDeviceClass.GAS,
125 native_unit_of_measurement=UnitOfVolume.CENTUM_CUBIC_FEET,
126 state_class=SensorStateClass.TOTAL,
127 suggested_display_precision=0,
128 value_fn=
lambda data: data.forecasted_usage,
131 key=
"gas_typical_usage",
132 name=
"Typical monthly gas usage",
133 device_class=SensorDeviceClass.GAS,
134 native_unit_of_measurement=UnitOfVolume.CENTUM_CUBIC_FEET,
135 state_class=SensorStateClass.TOTAL,
136 suggested_display_precision=0,
137 value_fn=
lambda data: data.typical_usage,
140 key=
"gas_cost_to_date",
141 name=
"Current bill gas cost to date",
142 device_class=SensorDeviceClass.MONETARY,
143 native_unit_of_measurement=
"USD",
144 state_class=SensorStateClass.TOTAL,
145 suggested_display_precision=0,
146 value_fn=
lambda data: data.cost_to_date,
149 key=
"gas_forecasted_cost",
150 name=
"Current bill gas forecasted cost",
151 device_class=SensorDeviceClass.MONETARY,
152 native_unit_of_measurement=
"USD",
153 state_class=SensorStateClass.TOTAL,
154 suggested_display_precision=0,
155 value_fn=
lambda data: data.forecasted_cost,
158 key=
"gas_typical_cost",
159 name=
"Typical monthly gas cost",
160 device_class=SensorDeviceClass.MONETARY,
161 native_unit_of_measurement=
"USD",
162 state_class=SensorStateClass.TOTAL,
163 suggested_display_precision=0,
164 value_fn=
lambda data: data.typical_cost,
167 key=
"gas_start_date",
168 name=
"Current bill gas start date",
169 device_class=SensorDeviceClass.DATE,
170 entity_category=EntityCategory.DIAGNOSTIC,
171 entity_registry_enabled_default=
False,
172 value_fn=
lambda data: data.start_date,
176 name=
"Current bill gas end date",
177 device_class=SensorDeviceClass.DATE,
178 entity_category=EntityCategory.DIAGNOSTIC,
179 entity_registry_enabled_default=
False,
180 value_fn=
lambda data: data.end_date,
186 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
188 """Set up the Opower sensor."""
190 coordinator: OpowerCoordinator = hass.data[DOMAIN][entry.entry_id]
191 entities: list[OpowerSensor] = []
192 forecasts = coordinator.data.values()
193 for forecast
in forecasts:
194 device_id = f
"{coordinator.api.utility.subdomain()}_{forecast.account.utility_account_id}"
196 identifiers={(DOMAIN, device_id)},
197 name=f
"{forecast.account.meter_type.name} account {forecast.account.utility_account_id}",
198 manufacturer=
"Opower",
199 model=coordinator.api.utility.name(),
200 entry_type=DeviceEntryType.SERVICE,
202 sensors: tuple[OpowerEntityDescription, ...] = ()
204 forecast.account.meter_type == MeterType.ELEC
205 and forecast.unit_of_measure == UnitOfMeasure.KWH
207 sensors = ELEC_SENSORS
209 forecast.account.meter_type == MeterType.GAS
210 and forecast.unit_of_measure
in [UnitOfMeasure.THERM, UnitOfMeasure.CCF]
212 sensors = GAS_SENSORS
217 forecast.account.utility_account_id,
221 for sensor
in sensors
228 """Representation of an Opower sensor."""
230 entity_description: OpowerEntityDescription
234 coordinator: OpowerCoordinator,
235 description: OpowerEntityDescription,
236 utility_account_id: str,
240 """Initialize the sensor."""
249 """Return the state."""
250 if self.coordinator.data
is not None:
None __init__(self, OpowerCoordinator coordinator, OpowerEntityDescription description, str utility_account_id, DeviceInfo device, str device_id)
StateType native_value(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)