Skip to content

Commit

Permalink
fix power sensor behavior when the state doesn't exists yet
Browse files Browse the repository at this point in the history
  • Loading branch information
litinoveweedle committed Jun 13, 2024
1 parent 0940b6b commit 0329481
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
11 changes: 6 additions & 5 deletions custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
)
Expand All @@ -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()
Expand All @@ -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)
11 changes: 6 additions & 5 deletions custom_components/smartir/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
)
Expand All @@ -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()
Expand All @@ -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)
11 changes: 6 additions & 5 deletions custom_components/smartir/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
)
Expand All @@ -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()
Expand All @@ -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)

0 comments on commit 0329481

Please sign in to comment.