Cleanup the min-max sensor (#41851)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
099de37ee5
commit
5f3fc6a9e3
1 changed files with 50 additions and 47 deletions
|
@ -77,10 +77,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||
|
||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
|
||||
async_add_entities(
|
||||
[MinMaxSensor(hass, entity_ids, name, sensor_type, round_digits)], True
|
||||
)
|
||||
return True
|
||||
async_add_entities([MinMaxSensor(entity_ids, name, sensor_type, round_digits)])
|
||||
|
||||
|
||||
def calc_min(sensor_values):
|
||||
|
@ -137,9 +134,8 @@ def calc_median(sensor_values, round_digits):
|
|||
class MinMaxSensor(Entity):
|
||||
"""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."""
|
||||
self._hass = hass
|
||||
self._entity_ids = entity_ids
|
||||
self._sensor_type = sensor_type
|
||||
self._round_digits = round_digits
|
||||
|
@ -155,50 +151,16 @@ class MinMaxSensor(Entity):
|
|||
self.count_sensors = len(self._entity_ids)
|
||||
self.states = {}
|
||||
|
||||
@callback
|
||||
def async_min_max_sensor_state_listener(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
|
||||
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)
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Handle added to Hass."""
|
||||
self.async_on_remove(
|
||||
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
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
|
@ -239,8 +201,49 @@ class MinMaxSensor(Entity):
|
|||
"""Return the icon to use in the frontend, if any."""
|
||||
return ICON
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the latest data and updates the states."""
|
||||
@callback
|
||||
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 = [
|
||||
(entity_id, self.states[entity_id])
|
||||
for entity_id in self._entity_ids
|
||||
|
|
Loading…
Add table
Reference in a new issue