Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restores switch state, case the switch is optimistic #14151

Merged
merged 6 commits into from
May 2, 2018

Conversation

dgomes
Copy link
Contributor

@dgomes dgomes commented Apr 28, 2018

Description:

When the switch works in optimistic mode, HA is unaware of the current switch state and always defaults to False on restart.

This pull request restores the state of the switch to the last state, which is somehow what is expected from an optimistic switch (that he has kept the state since the last restart and not defaulted to False)

Example entry for configuration.yaml (if applicable):

switch:
  - platform: mqtt
    name: teste_switch
    command_topic: test/dummy_switch
    optimistic: true

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

@dgomes
Copy link
Contributor Author

dgomes commented Apr 28, 2018

If this PR gets through I will gladly update the mqtt platforms in the light component.

@dgomes dgomes changed the title Restores states in case the switch is optimistic Restores switch state, case the switch is optimistic Apr 28, 2018
self.entity_id)
if last_state:
self._state = last_state.state
self.async_schedule_update_ha_state()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to schedule an update. We write the entity to the state machine right after this method is done.

Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to merge when a test is added.

self.hass, self._state_topic, state_message_received,
self._qos)

if self._optimistic:
last_state = await async_get_last_state(self.hass,
self.entity_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line over-indented for visual indent

@@ -39,8 +39,7 @@
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)


@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (85 > 79 characters)

@balloob balloob merged commit c851dfa into home-assistant:dev May 2, 2018
@balloob balloob mentioned this pull request May 11, 2018
@dgomes dgomes mentioned this pull request May 11, 2018
8 tasks
@dgomes dgomes mentioned this pull request May 28, 2018
3 tasks
@dgomes dgomes deleted the mqtt_restore_state branch August 17, 2018 22:09
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants