Write state if schedule update state from async context (#31758)

* Write state if schedule update state from async context

* Fix most tests

* Fix test and PS4 I/O in event loop

* Fix ps4 better
This commit is contained in:
Paulus Schoutsen 2020-02-13 10:22:06 -08:00 committed by GitHub
parent 8d1f8055dd
commit 9e7185c676
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 16 additions and 12 deletions

View File

@ -98,7 +98,7 @@ class HomematicipAlarmControlPanel(AlarmControlPanel):
def _async_device_changed(self, *args, **kwargs) -> None: def _async_device_changed(self, *args, **kwargs) -> None:
"""Handle device state changes.""" """Handle device state changes."""
_LOGGER.debug("Event %s (%s)", self.name, CONST_ALARM_CONTROL_PANEL_NAME) _LOGGER.debug("Event %s (%s)", self.name, CONST_ALARM_CONTROL_PANEL_NAME)
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state(True)
@property @property
def name(self) -> str: def name(self) -> str:

View File

@ -97,11 +97,7 @@ class PS4Device(MediaPlayerDevice):
def status_callback(self): def status_callback(self):
"""Handle status callback. Parse status.""" """Handle status callback. Parse status."""
self._parse_status() self._parse_status()
self.async_write_ha_state()
@callback
def schedule_update(self):
"""Schedules update with HA."""
self.async_schedule_update_ha_state()
@callback @callback
def subscribe_to_protocol(self): def subscribe_to_protocol(self):
@ -184,7 +180,6 @@ class PS4Device(MediaPlayerDevice):
self._media_content_id = title_id self._media_content_id = title_id
if self._use_saved(): if self._use_saved():
_LOGGER.debug("Using saved data for media: %s", title_id) _LOGGER.debug("Using saved data for media: %s", title_id)
self.schedule_update()
return return
self._media_title = name self._media_title = name
@ -223,13 +218,11 @@ class PS4Device(MediaPlayerDevice):
"""Set states for state idle.""" """Set states for state idle."""
self.reset_title() self.reset_title()
self._state = STATE_IDLE self._state = STATE_IDLE
self.schedule_update()
def state_standby(self): def state_standby(self):
"""Set states for state standby.""" """Set states for state standby."""
self.reset_title() self.reset_title()
self._state = STATE_STANDBY self._state = STATE_STANDBY
self.schedule_update()
def state_unknown(self): def state_unknown(self):
"""Set states for state unknown.""" """Set states for state unknown."""
@ -286,8 +279,8 @@ class PS4Device(MediaPlayerDevice):
self._media_image = art or None self._media_image = art or None
self._media_type = media_type self._media_type = media_type
self.update_list() await self.hass.async_add_executor_job(self.update_list)
self.schedule_update() self.async_write_ha_state()
def update_list(self): def update_list(self):
"""Update Game List, Correct data if different.""" """Update Game List, Correct data if different."""

View File

@ -441,7 +441,10 @@ class Entity(ABC):
If state is changed more than once before the ha state change task has If state is changed more than once before the ha state change task has
been executed, the intermediate state transitions will be missed. been executed, the intermediate state transitions will be missed.
""" """
self.hass.async_create_task(self.async_update_ha_state(force_refresh)) if force_refresh:
self.hass.async_create_task(self.async_update_ha_state(force_refresh))
else:
self.async_write_ha_state()
async def async_device_update(self, warning=True): async def async_device_update(self, warning=True):
"""Process 'update' or 'async_update' from entity. """Process 'update' or 'async_update' from entity.

View File

@ -187,6 +187,7 @@ async def test_lights_turn_on_when_coming_home_after_sun_set_person(hass, scanne
# person home switches on # person home switches on
hass.states.async_set(device_1, STATE_HOME) hass.states.async_set(device_1, STATE_HOME)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
assert all( assert all(
light.is_on(hass, ent_id) light.is_on(hass, ent_id)

View File

@ -130,6 +130,7 @@ async def test_off_delay(hass, monkeypatch):
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
event_callback(on_event) event_callback(on_event)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test2") state = hass.states.get("binary_sensor.test2")
assert state.state == STATE_ON assert state.state == STATE_ON
assert len(events) == 1 assert len(events) == 1
@ -140,6 +141,7 @@ async def test_off_delay(hass, monkeypatch):
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
event_callback(on_event) event_callback(on_event)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test2") state = hass.states.get("binary_sensor.test2")
assert state.state == STATE_ON assert state.state == STATE_ON
assert len(events) == 2 assert len(events) == 2
@ -149,6 +151,7 @@ async def test_off_delay(hass, monkeypatch):
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=future): with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=future):
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test2") state = hass.states.get("binary_sensor.test2")
assert state.state == STATE_ON assert state.state == STATE_ON
assert len(events) == 2 assert len(events) == 2
@ -158,6 +161,7 @@ async def test_off_delay(hass, monkeypatch):
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=future): with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=future):
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test2") state = hass.states.get("binary_sensor.test2")
assert state.state == STATE_OFF assert state.state == STATE_OFF
assert len(events) == 3 assert len(events) == 3

View File

@ -144,6 +144,7 @@ async def test_firing_bus_event(hass, monkeypatch):
# test event for new unconfigured sensor # test event for new unconfigured sensor
event_callback({"id": "protocol_0_0", "command": "down"}) event_callback({"id": "protocol_0_0", "command": "down"})
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
assert calls[0].data == {"state": "down", "entity_id": DOMAIN + ".test"} assert calls[0].data == {"state": "down", "entity_id": DOMAIN + ".test"}

View File

@ -184,6 +184,7 @@ async def test_firing_bus_event(hass, monkeypatch):
# test event for new unconfigured sensor # test event for new unconfigured sensor
event_callback({"id": "protocol_0_0", "command": "off"}) event_callback({"id": "protocol_0_0", "command": "off"})
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
assert calls[0].data == {"state": "off", "entity_id": DOMAIN + ".test"} assert calls[0].data == {"state": "off", "entity_id": DOMAIN + ".test"}

View File

@ -214,6 +214,7 @@ async def test_device_defaults(hass, monkeypatch):
# test event for new unconfigured sensor # test event for new unconfigured sensor
event_callback({"id": "protocol_0_0", "command": "off"}) event_callback({"id": "protocol_0_0", "command": "off"})
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
assert calls[0].data == {"state": "off", "entity_id": DOMAIN + ".test"} assert calls[0].data == {"state": "off", "entity_id": DOMAIN + ".test"}