1 """Support for compensation sensor."""
3 from __future__
import annotations
12 ATTR_UNIT_OF_MEASUREMENT,
18 CONF_UNIT_OF_MEASUREMENT,
23 EventStateChangedData,
40 _LOGGER = logging.getLogger(__name__)
42 ATTR_COEFFICIENTS =
"coefficients"
43 ATTR_SOURCE =
"source"
44 ATTR_SOURCE_ATTRIBUTE =
"source_attribute"
50 async_add_entities: AddEntitiesCallback,
51 discovery_info: DiscoveryInfoType |
None =
None,
53 """Set up the Compensation sensor."""
54 if discovery_info
is None:
57 compensation: str = discovery_info[CONF_COMPENSATION]
58 conf: dict[str, Any] = hass.data[DATA_COMPENSATION][compensation]
60 source: str = conf[CONF_SOURCE]
61 attribute: str |
None = conf.get(CONF_ATTRIBUTE)
62 name = f
"{DEFAULT_NAME} {source}"
63 if attribute
is not None:
64 name = f
"{name} {attribute}"
69 conf.get(CONF_UNIQUE_ID),
74 conf[CONF_POLYNOMIAL],
75 conf.get(CONF_UNIT_OF_MEASUREMENT),
84 """Representation of a Compensation sensor."""
86 _attr_should_poll =
False
90 unique_id: str |
None,
93 attribute: str |
None,
95 polynomial: np.poly1d,
96 unit_of_measurement: str |
None,
97 minimum: tuple[float, float] |
None,
98 maximum: tuple[float, float] |
None,
100 """Initialize the Compensation sensor."""
113 """Handle added to Hass."""
124 """Return the state attributes of the sensor."""
135 self, event: Event[EventStateChangedData]
137 """Handle sensor state changes."""
138 new_state: State |
None
139 if (new_state := event.data[
"new_state"])
is None:
144 ATTR_UNIT_OF_MEASUREMENT
150 value =
None if new_state.state == STATE_UNKNOWN
else new_state.state
152 x_value =
float(value)
153 if self.
_minimum_minimum
is not None and x_value <= self.
_minimum_minimum[0]:
155 elif self.
_maximum_maximum
is not None and x_value >= self.
_maximum_maximum[0]:
158 y_value = self.
_poly_poly(x_value)
161 except (ValueError, TypeError):
165 "%s attribute %s is not numerical",
170 _LOGGER.warning(
"%s state is not numerical", self.
_source_entity_id_source_entity_id)
None __init__(self, str|None unique_id, str name, str source, str|None attribute, int precision, np.poly1d polynomial, str|None unit_of_measurement, tuple[float, float]|None minimum, tuple[float, float]|None maximum)
dict[str, Any] extra_state_attributes(self)
_attr_native_unit_of_measurement
None async_added_to_hass(self)
None _async_compensation_sensor_state_listener(self, Event[EventStateChangedData] event)
str|None native_unit_of_measurement(self)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
CALLBACK_TYPE async_track_state_change_event(HomeAssistant hass, str|Iterable[str] entity_ids, Callable[[Event[EventStateChangedData]], Any] action, HassJobType|None job_type=None)