From 0329481e6831613373af7b9f5c37119491d7b452 Mon Sep 17 00:00:00 2001 From: litinoveweedle <15144712+litinoveweedle@users.noreply.github.com> Date: Thu, 13 Jun 2024 23:14:09 +0200 Subject: [PATCH] fix power sensor behavior when the state doesn't exists yet --- custom_components/smartir/climate.py | 11 ++++++----- custom_components/smartir/fan.py | 11 ++++++----- custom_components/smartir/media_player.py | 11 ++++++----- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/custom_components/smartir/climate.py b/custom_components/smartir/climate.py index 3d8c8201..86d9c8d9 100644 --- a/custom_components/smartir/climate.py +++ b/custom_components/smartir/climate.py @@ -260,7 +260,6 @@ async def async_added_to_hass(self): if self._power_sensor: self._on_by_remote = last_state.attributes.get("on_by_remote", False) - self._async_power_sensor_check_schedule(self._state) if self._temperature_sensor: async_track_state_change_event( @@ -722,9 +721,11 @@ def _async_power_sensor_check(*_): self._power_sensor_check_cancel = None expected_state = self._power_sensor_check_expect self._power_sensor_check_expect = None - current_state = self.hass.states.get(self._power_sensor).state + current_state = getattr( + self.hass.states.get(self._power_sensor), "state", None + ) _LOGGER.debug( - "Executing power sensor check for expected state '%s', current state '%s'", + "Executing power sensor check for expected state '%s', current state '%s'.", expected_state, current_state, ) @@ -736,7 +737,7 @@ def _async_power_sensor_check(*_): ): self._state = current_state _LOGGER.debug( - "Power sensor check failed, reverted device state to '%s'", + "Power sensor check failed, reverted device state to '%s'.", self._state, ) self.async_write_ha_state() @@ -745,4 +746,4 @@ def _async_power_sensor_check(*_): self._power_sensor_check_cancel = async_call_later( self.hass, self._power_sensor_delay, _async_power_sensor_check ) - _LOGGER.debug("Scheduled power sensor check for '%s' state", state) + _LOGGER.debug("Scheduled power sensor check for '%s' state.", state) diff --git a/custom_components/smartir/fan.py b/custom_components/smartir/fan.py index c5396974..82f8e927 100644 --- a/custom_components/smartir/fan.py +++ b/custom_components/smartir/fan.py @@ -164,7 +164,6 @@ async def async_added_to_hass(self): if self._power_sensor: self._on_by_remote = last_state.attributes.get("on_by_remote", False) - self._async_power_sensor_check_schedule(self._state) if self._power_sensor: async_track_state_change_event( @@ -359,9 +358,11 @@ def _async_power_sensor_check(*_): self._power_sensor_check_cancel = None expected_state = self._power_sensor_check_expect self._power_sensor_check_expect = None - current_state = self.hass.states.get(self._power_sensor).state + current_state = getattr( + self.hass.states.get(self._power_sensor), "state", None + ) _LOGGER.debug( - "Executing power sensor check for expected state '%s', current state '%s'", + "Executing power sensor check for expected state '%s', current state '%s'.", expected_state, current_state, ) @@ -373,7 +374,7 @@ def _async_power_sensor_check(*_): ): self._state = current_state _LOGGER.debug( - "Power sensor check failed, reverted device state to '%s'", + "Power sensor check failed, reverted device state to '%s'.", self._state, ) self.async_write_ha_state() @@ -382,4 +383,4 @@ def _async_power_sensor_check(*_): self._power_sensor_check_cancel = async_call_later( self.hass, self._power_sensor_delay, _async_power_sensor_check ) - _LOGGER.debug("Scheduled power sensor check for '%s' state", state) + _LOGGER.debug("Scheduled power sensor check for '%s' state.", state) diff --git a/custom_components/smartir/media_player.py b/custom_components/smartir/media_player.py index 93ceee71..d9d3edb5 100644 --- a/custom_components/smartir/media_player.py +++ b/custom_components/smartir/media_player.py @@ -185,7 +185,6 @@ async def async_added_to_hass(self): if self._power_sensor: self._on_by_remote = last_state.attributes.get("on_by_remote", False) - self._async_power_sensor_check_schedule(self._state) if self._power_sensor: async_track_state_change_event( @@ -389,9 +388,11 @@ def _async_power_sensor_check(*_): self._power_sensor_check_cancel = None expected_state = self._power_sensor_check_expect self._power_sensor_check_expect = None - current_state = self.hass.states.get(self._power_sensor).state + current_state = getattr( + self.hass.states.get(self._power_sensor), "state", None + ) _LOGGER.debug( - "Executing power sensor check for expected state '%s', current state '%s'", + "Executing power sensor check for expected state '%s', current state '%s'.", expected_state, current_state, ) @@ -403,7 +404,7 @@ def _async_power_sensor_check(*_): ): self._state = current_state _LOGGER.debug( - "Power sensor check failed, reverted device state to '%s'", + "Power sensor check failed, reverted device state to '%s'.", self._state, ) self.async_write_ha_state() @@ -412,4 +413,4 @@ def _async_power_sensor_check(*_): self._power_sensor_check_cancel = async_call_later( self.hass, self._power_sensor_delay, _async_power_sensor_check ) - _LOGGER.debug("Scheduled power sensor check for '%s' state", state) + _LOGGER.debug("Scheduled power sensor check for '%s' state.", state)