Skip to content

Commit

Permalink
Merge pull request #141 from litinoveweedle/fix_state_handling
Browse files Browse the repository at this point in the history
Fix state handling
  • Loading branch information
litinoveweedle committed Sep 27, 2024
2 parents a5ddb97 + fa27343 commit 9d5e3f8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
7 changes: 4 additions & 3 deletions custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ async def async_setup_platform(

class SmartIRClimate(ClimateEntity, RestoreEntity):
_attr_should_poll = False
_attr_assumed_state = True
_enable_turn_on_off_backwards_compatibility = False

def __init__(self, hass, config, device_data):
Expand All @@ -113,7 +114,7 @@ def __init__(self, hass, config, device_data):
self._power_sensor_restore_state = config.get(CONF_POWER_SENSOR_RESTORE_STATE)
self._temperature_unit = hass.config.units.temperature_unit

self._state = STATE_UNKNOWN
self._state = STATE_OFF
self._hvac_mode = None
self._preset_mode = None
self._fan_mode = None
Expand Down Expand Up @@ -826,13 +827,13 @@ async def _async_power_sensor_changed(
if old_state is not None and new_state.state == old_state.state:
return

if new_state.state == STATE_ON and self._state == STATE_OFF:
if new_state.state == STATE_ON and self._state != STATE_ON:
self._state = STATE_ON
self._on_by_remote = True
await self._async_update_hvac_action()
elif new_state.state == STATE_OFF:
self._on_by_remote = False
if self._state == STATE_ON:
if self._state != STATE_OFF:
self._state = STATE_OFF
await self._async_update_hvac_action()
self.async_write_ha_state()
Expand Down
7 changes: 4 additions & 3 deletions custom_components/smartir/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ async def async_setup_platform(

class SmartIRFan(FanEntity, RestoreEntity):
_attr_should_poll = False
_attr_assumed_state = True
_enable_turn_on_off_backwards_compatibility = False

def __init__(self, hass, config, device_data):
Expand All @@ -89,7 +90,7 @@ def __init__(self, hass, config, device_data):
self._power_sensor_delay = config.get(CONF_POWER_SENSOR_DELAY)
self._power_sensor_restore_state = config.get(CONF_POWER_SENSOR_RESTORE_STATE)

self._state = STATE_UNKNOWN
self._state = STATE_OFF
self._speed = None
self._oscillating = None
self._on_by_remote = False
Expand Down Expand Up @@ -340,12 +341,12 @@ async def _async_power_sensor_changed(
if old_state is not None and new_state.state == old_state.state:
return

if new_state.state == STATE_ON and self._state == STATE_OFF:
if new_state.state == STATE_ON and self._state != STATE_ON:
self._state = STATE_ON
self._on_by_remote = True
elif new_state.state == STATE_OFF:
self._on_by_remote = False
if self._state == STATE_ON:
if self._state != STATE_OFF:
self._state = STATE_OFF
self.async_write_ha_state()

Expand Down
20 changes: 10 additions & 10 deletions custom_components/smartir/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(self, hass, config, device_data):
self._power_sensor_delay = config.get(CONF_POWER_SENSOR_DELAY)
self._power_sensor_restore_state = config.get(CONF_POWER_SENSOR_RESTORE_STATE)

self._power = STATE_ON
self._state = STATE_OFF
self._brightness = None
self._colortemp = None
self._on_by_remote = False
Expand Down Expand Up @@ -144,7 +144,7 @@ async def async_added_to_hass(self):

last_state = await self.async_get_last_state()
if last_state is not None:
self._power = last_state.state
self._state = last_state.state
if ATTR_BRIGHTNESS in last_state.attributes:
self._brightness = last_state.attributes[ATTR_BRIGHTNESS]
if ATTR_COLOR_TEMP_KELVIN in last_state.attributes:
Expand Down Expand Up @@ -186,7 +186,7 @@ def max_color_temp_kelvin(self):

@property
def is_on(self):
return self._power == STATE_ON or self._on_by_remote
return self._state == STATE_ON or self._on_by_remote

@property
def brightness(self):
Expand All @@ -207,8 +207,8 @@ def extra_state_attributes(self):
async def async_turn_on(self, **params):
did_something = False
# Turn the light on if off
if self._power != STATE_ON and not self._on_by_remote:
self._power = STATE_ON
if self._state != STATE_ON and not self._on_by_remote:
self._state = STATE_ON
did_something = True
await self.send_command(CMD_POWER_ON)

Expand Down Expand Up @@ -245,7 +245,7 @@ async def async_turn_on(self, **params):
# when a nightlight is fitted for brightness of 1
if params.get(ATTR_BRIGHTNESS) == 1 and CMD_NIGHTLIGHT in self._commands:
self._brightness = 1
self._power = STATE_ON
self._state = STATE_ON
did_something = True
await self.send_command(CMD_NIGHTLIGHT)

Expand Down Expand Up @@ -286,13 +286,13 @@ async def async_turn_on(self, **params):
# If we do have such monitoring, avoid issuing the command in case
# on and off are the same remote code.
if not did_something and not self._on_by_remote:
self._power = STATE_ON
self._state = STATE_ON
await self.send_command(CMD_POWER_ON)

self.async_write_ha_state()

async def async_turn_off(self):
self._power = STATE_OFF
self._state = STATE_OFF
await self.send_command(CMD_POWER_OFF)
self.async_write_ha_state()

Expand Down Expand Up @@ -325,12 +325,12 @@ async def _async_power_sensor_changed(
if old_state is not None and new_state.state == old_state.state:
return

if new_state.state == STATE_ON and self._state == STATE_OFF:
if new_state.state == STATE_ON and self._state != STATE_ON:
self._state = STATE_ON
self._on_by_remote = True
elif new_state.state == STATE_OFF:
self._on_by_remote = False
if self._state == STATE_ON:
if self._state != STATE_OFF:
self._state = STATE_OFF
self.async_write_ha_state()

Expand Down
7 changes: 4 additions & 3 deletions custom_components/smartir/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async def async_setup_platform(

class SmartIRMediaPlayer(MediaPlayerEntity, RestoreEntity):
_attr_should_poll = False
_attr_assumed_state = True

def __init__(self, hass, config, device_data):
self.hass = hass
Expand All @@ -86,7 +87,7 @@ def __init__(self, hass, config, device_data):
self._power_sensor_restore_state = config.get(CONF_POWER_SENSOR_RESTORE_STATE)
self._device_class = config.get(CONF_DEVICE_CLASS)

self._state = STATE_UNKNOWN
self._state = STATE_OFF
self._sources_list = []
self._source = None
self._on_by_remote = False
Expand Down Expand Up @@ -362,12 +363,12 @@ async def _async_power_sensor_changed(
if old_state is not None and new_state.state == old_state.state:
return

if new_state.state == STATE_ON and self._state == STATE_OFF:
if new_state.state == STATE_ON and self._state != STATE_ON:
self._on_by_remote = True
self._state = STATE_ON
elif new_state.state == STATE_OFF:
self._on_by_remote = False
if self._state == STATE_ON:
if self._state != STATE_OFF:
self._state = STATE_OFF
# self._source = None
self.async_write_ha_state()
Expand Down

0 comments on commit 9d5e3f8

Please sign in to comment.