Ensure harmony callbacks run in the event loop (#49450)

pull/49053/head
J. Nick Koston 2021-04-20 03:09:46 -10:00 committed by GitHub
parent c14e525ac3
commit 05982ffc60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 18 deletions

View File

@ -16,14 +16,14 @@ class ConnectionStateMixin:
super().__init__()
self._unsub_mark_disconnected = None
async def got_connected(self, _=None):
async def async_got_connected(self, _=None):
"""Notification that we're connected to the HUB."""
_LOGGER.debug("%s: connected to the HUB", self._name)
self.async_write_ha_state()
self._clear_disconnection_delay()
async def got_disconnected(self, _=None):
async def async_got_disconnected(self, _=None):
"""Notification that we're disconnected from the HUB."""
_LOGGER.debug("%s: disconnected from the HUB", self._name)
# We're going to wait for 10 seconds before announcing we're

View File

@ -16,7 +16,7 @@ from homeassistant.components.remote import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -115,16 +115,17 @@ class HarmonyRemote(ConnectionStateMixin, remote.RemoteEntity, RestoreEntity):
def _setup_callbacks(self):
callbacks = {
"connected": self.got_connected,
"disconnected": self.got_disconnected,
"config_updated": self.new_config,
"activity_starting": self.new_activity,
"activity_started": self._new_activity_finished,
"connected": self.async_got_connected,
"disconnected": self.async_got_disconnected,
"config_updated": self.async_new_config,
"activity_starting": self.async_new_activity,
"activity_started": self.async_new_activity_finished,
}
self.async_on_remove(self._data.async_subscribe(HarmonyCallback(**callbacks)))
def _new_activity_finished(self, activity_info: tuple) -> None:
@callback
def async_new_activity_finished(self, activity_info: tuple) -> None:
"""Call for finished updated current activity."""
self._activity_starting = None
self.async_write_ha_state()
@ -148,7 +149,7 @@ class HarmonyRemote(ConnectionStateMixin, remote.RemoteEntity, RestoreEntity):
# Store Harmony HUB config, this will also update our current
# activity
await self.new_config()
await self.async_new_config()
# Restore the last activity so we know
# how what to turn on if nothing
@ -212,7 +213,8 @@ class HarmonyRemote(ConnectionStateMixin, remote.RemoteEntity, RestoreEntity):
"""Return True if connected to Hub, otherwise False."""
return self._data.available
def new_activity(self, activity_info: tuple) -> None:
@callback
def async_new_activity(self, activity_info: tuple) -> None:
"""Call for updating the current activity."""
activity_id, activity_name = activity_info
_LOGGER.debug("%s: activity reported as: %s", self._name, activity_name)
@ -229,10 +231,10 @@ class HarmonyRemote(ConnectionStateMixin, remote.RemoteEntity, RestoreEntity):
self._state = bool(activity_id != -1)
self.async_write_ha_state()
async def new_config(self, _=None):
async def async_new_config(self, _=None):
"""Call for updating the current activity."""
_LOGGER.debug("%s: configuration has been updated", self._name)
self.new_activity(self._data.current_activity)
self.async_new_activity(self._data.current_activity)
await self.hass.async_add_executor_job(self.write_config_file)
async def async_turn_on(self, **kwargs):

View File

@ -3,6 +3,7 @@ import logging
from homeassistant.components.switch import SwitchEntity
from homeassistant.const import CONF_NAME
from homeassistant.core import callback
from .connection_state import ConnectionStateMixin
from .const import DOMAIN, HARMONY_DATA
@ -80,14 +81,15 @@ class HarmonyActivitySwitch(ConnectionStateMixin, SwitchEntity):
"""Call when entity is added to hass."""
callbacks = {
"connected": self.got_connected,
"disconnected": self.got_disconnected,
"activity_starting": self._activity_update,
"activity_started": self._activity_update,
"connected": self.async_got_connected,
"disconnected": self.async_got_disconnected,
"activity_starting": self._async_activity_update,
"activity_started": self._async_activity_update,
"config_updated": None,
}
self.async_on_remove(self._data.async_subscribe(HarmonyCallback(**callbacks)))
def _activity_update(self, activity_info: tuple):
@callback
def _async_activity_update(self, activity_info: tuple):
self.async_write_ha_state()