From 898a07fa75c010b29aecc8a35c6190d5564dd536 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 28 Nov 2020 21:19:10 +0100 Subject: [PATCH] Make threshold binary sensor faster (#43695) --- homeassistant/components/threshold/binary_sensor.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/threshold/binary_sensor.py b/homeassistant/components/threshold/binary_sensor.py index 55ac21f6f13..fa05fc09687 100644 --- a/homeassistant/components/threshold/binary_sensor.py +++ b/homeassistant/components/threshold/binary_sensor.py @@ -71,7 +71,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= hass, entity_id, name, lower, upper, hysteresis, device_class ) ], - True, ) @@ -88,8 +87,8 @@ class ThresholdSensor(BinarySensorEntity): self._hysteresis = hysteresis self._device_class = device_class - self._state_position = None - self._state = False + self._state_position = POSITION_UNKNOWN + self._state = None self.sensor_value = None @callback @@ -107,7 +106,8 @@ class ThresholdSensor(BinarySensorEntity): self.sensor_value = None _LOGGER.warning("State is not numerical") - hass.async_add_job(self.async_update_ha_state, True) + self._update_state() + self.async_write_ha_state() async_track_state_change_event( hass, [entity_id], async_threshold_sensor_state_listener @@ -156,8 +156,9 @@ class ThresholdSensor(BinarySensorEntity): ATTR_UPPER: self._threshold_upper, } - async def async_update(self): - """Get the latest data and updates the states.""" + @callback + def _update_state(self): + """Update the state.""" def below(threshold): """Determine if the sensor value is below a threshold."""