mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Cleanup the min-max sensor (#41851)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
099de37ee5
commit
5f3fc6a9e3
@ -77,10 +77,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
|
|
||||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities([MinMaxSensor(entity_ids, name, sensor_type, round_digits)])
|
||||||
[MinMaxSensor(hass, entity_ids, name, sensor_type, round_digits)], True
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def calc_min(sensor_values):
|
def calc_min(sensor_values):
|
||||||
@ -137,9 +134,8 @@ def calc_median(sensor_values, round_digits):
|
|||||||
class MinMaxSensor(Entity):
|
class MinMaxSensor(Entity):
|
||||||
"""Representation of a min/max sensor."""
|
"""Representation of a min/max sensor."""
|
||||||
|
|
||||||
def __init__(self, hass, entity_ids, name, sensor_type, round_digits):
|
def __init__(self, entity_ids, name, sensor_type, round_digits):
|
||||||
"""Initialize the min/max sensor."""
|
"""Initialize the min/max sensor."""
|
||||||
self._hass = hass
|
|
||||||
self._entity_ids = entity_ids
|
self._entity_ids = entity_ids
|
||||||
self._sensor_type = sensor_type
|
self._sensor_type = sensor_type
|
||||||
self._round_digits = round_digits
|
self._round_digits = round_digits
|
||||||
@ -155,50 +151,16 @@ class MinMaxSensor(Entity):
|
|||||||
self.count_sensors = len(self._entity_ids)
|
self.count_sensors = len(self._entity_ids)
|
||||||
self.states = {}
|
self.states = {}
|
||||||
|
|
||||||
@callback
|
async def async_added_to_hass(self):
|
||||||
def async_min_max_sensor_state_listener(event):
|
"""Handle added to Hass."""
|
||||||
"""Handle the sensor state changes."""
|
|
||||||
new_state = event.data.get("new_state")
|
|
||||||
entity = event.data.get("entity_id")
|
|
||||||
|
|
||||||
if new_state.state is None or new_state.state in [
|
|
||||||
STATE_UNKNOWN,
|
|
||||||
STATE_UNAVAILABLE,
|
|
||||||
]:
|
|
||||||
self.states[entity] = STATE_UNKNOWN
|
|
||||||
hass.async_add_job(self.async_update_ha_state, True)
|
|
||||||
return
|
|
||||||
|
|
||||||
if self._unit_of_measurement is None:
|
|
||||||
self._unit_of_measurement = new_state.attributes.get(
|
|
||||||
ATTR_UNIT_OF_MEASUREMENT
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._unit_of_measurement != new_state.attributes.get(
|
|
||||||
ATTR_UNIT_OF_MEASUREMENT
|
|
||||||
):
|
|
||||||
_LOGGER.warning(
|
|
||||||
"Units of measurement do not match for entity %s", self.entity_id
|
|
||||||
)
|
|
||||||
self._unit_of_measurement_mismatch = True
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.states[entity] = float(new_state.state)
|
|
||||||
self.last = float(new_state.state)
|
|
||||||
self.last_entity_id = entity
|
|
||||||
except ValueError:
|
|
||||||
_LOGGER.warning(
|
|
||||||
"Unable to store state. Only numerical states are supported"
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.async_add_job(self.async_update_ha_state, True)
|
|
||||||
|
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
async_track_state_change_event(
|
async_track_state_change_event(
|
||||||
hass, entity_ids, async_min_max_sensor_state_listener
|
self.hass, self._entity_ids, self._async_min_max_sensor_state_listener
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self._calc_values()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
@ -239,8 +201,49 @@ class MinMaxSensor(Entity):
|
|||||||
"""Return the icon to use in the frontend, if any."""
|
"""Return the icon to use in the frontend, if any."""
|
||||||
return ICON
|
return ICON
|
||||||
|
|
||||||
async def async_update(self):
|
@callback
|
||||||
"""Get the latest data and updates the states."""
|
def _async_min_max_sensor_state_listener(self, event):
|
||||||
|
"""Handle the sensor state changes."""
|
||||||
|
new_state = event.data.get("new_state")
|
||||||
|
entity = event.data.get("entity_id")
|
||||||
|
|
||||||
|
if new_state.state is None or new_state.state in [
|
||||||
|
STATE_UNKNOWN,
|
||||||
|
STATE_UNAVAILABLE,
|
||||||
|
]:
|
||||||
|
self.states[entity] = STATE_UNKNOWN
|
||||||
|
self._calc_values()
|
||||||
|
self.async_write_ha_state()
|
||||||
|
return
|
||||||
|
|
||||||
|
if self._unit_of_measurement is None:
|
||||||
|
self._unit_of_measurement = new_state.attributes.get(
|
||||||
|
ATTR_UNIT_OF_MEASUREMENT
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._unit_of_measurement != new_state.attributes.get(
|
||||||
|
ATTR_UNIT_OF_MEASUREMENT
|
||||||
|
):
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Units of measurement do not match for entity %s", self.entity_id
|
||||||
|
)
|
||||||
|
self._unit_of_measurement_mismatch = True
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.states[entity] = float(new_state.state)
|
||||||
|
self.last = float(new_state.state)
|
||||||
|
self.last_entity_id = entity
|
||||||
|
except ValueError:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Unable to store state. Only numerical states are supported"
|
||||||
|
)
|
||||||
|
|
||||||
|
self._calc_values()
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _calc_values(self):
|
||||||
|
"""Calculate the values."""
|
||||||
sensor_values = [
|
sensor_values = [
|
||||||
(entity_id, self.states[entity_id])
|
(entity_id, self.states[entity_id])
|
||||||
for entity_id in self._entity_ids
|
for entity_id in self._entity_ids
|
||||||
|
Loading…
x
Reference in New Issue
Block a user