Ensure harmony callbacks run in the event loop (#49450)
This commit is contained in:
parent
c14e525ac3
commit
05982ffc60
3 changed files with 22 additions and 18 deletions
|
@ -16,14 +16,14 @@ class ConnectionStateMixin:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._unsub_mark_disconnected = None
|
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."""
|
"""Notification that we're connected to the HUB."""
|
||||||
_LOGGER.debug("%s: connected to the HUB", self._name)
|
_LOGGER.debug("%s: connected to the HUB", self._name)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
self._clear_disconnection_delay()
|
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."""
|
"""Notification that we're disconnected from the HUB."""
|
||||||
_LOGGER.debug("%s: disconnected from the HUB", self._name)
|
_LOGGER.debug("%s: disconnected from the HUB", self._name)
|
||||||
# We're going to wait for 10 seconds before announcing we're
|
# We're going to wait for 10 seconds before announcing we're
|
||||||
|
|
|
@ -16,7 +16,7 @@ from homeassistant.components.remote import (
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.const import ATTR_ENTITY_ID
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
@ -115,16 +115,17 @@ class HarmonyRemote(ConnectionStateMixin, remote.RemoteEntity, RestoreEntity):
|
||||||
|
|
||||||
def _setup_callbacks(self):
|
def _setup_callbacks(self):
|
||||||
callbacks = {
|
callbacks = {
|
||||||
"connected": self.got_connected,
|
"connected": self.async_got_connected,
|
||||||
"disconnected": self.got_disconnected,
|
"disconnected": self.async_got_disconnected,
|
||||||
"config_updated": self.new_config,
|
"config_updated": self.async_new_config,
|
||||||
"activity_starting": self.new_activity,
|
"activity_starting": self.async_new_activity,
|
||||||
"activity_started": self._new_activity_finished,
|
"activity_started": self.async_new_activity_finished,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.async_on_remove(self._data.async_subscribe(HarmonyCallback(**callbacks)))
|
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."""
|
"""Call for finished updated current activity."""
|
||||||
self._activity_starting = None
|
self._activity_starting = None
|
||||||
self.async_write_ha_state()
|
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
|
# Store Harmony HUB config, this will also update our current
|
||||||
# activity
|
# activity
|
||||||
await self.new_config()
|
await self.async_new_config()
|
||||||
|
|
||||||
# Restore the last activity so we know
|
# Restore the last activity so we know
|
||||||
# how what to turn on if nothing
|
# 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 True if connected to Hub, otherwise False."""
|
||||||
return self._data.available
|
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."""
|
"""Call for updating the current activity."""
|
||||||
activity_id, activity_name = activity_info
|
activity_id, activity_name = activity_info
|
||||||
_LOGGER.debug("%s: activity reported as: %s", self._name, activity_name)
|
_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._state = bool(activity_id != -1)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def new_config(self, _=None):
|
async def async_new_config(self, _=None):
|
||||||
"""Call for updating the current activity."""
|
"""Call for updating the current activity."""
|
||||||
_LOGGER.debug("%s: configuration has been updated", self._name)
|
_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)
|
await self.hass.async_add_executor_job(self.write_config_file)
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
|
|
|
@ -3,6 +3,7 @@ import logging
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity
|
from homeassistant.components.switch import SwitchEntity
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
|
from homeassistant.core import callback
|
||||||
|
|
||||||
from .connection_state import ConnectionStateMixin
|
from .connection_state import ConnectionStateMixin
|
||||||
from .const import DOMAIN, HARMONY_DATA
|
from .const import DOMAIN, HARMONY_DATA
|
||||||
|
@ -80,14 +81,15 @@ class HarmonyActivitySwitch(ConnectionStateMixin, SwitchEntity):
|
||||||
"""Call when entity is added to hass."""
|
"""Call when entity is added to hass."""
|
||||||
|
|
||||||
callbacks = {
|
callbacks = {
|
||||||
"connected": self.got_connected,
|
"connected": self.async_got_connected,
|
||||||
"disconnected": self.got_disconnected,
|
"disconnected": self.async_got_disconnected,
|
||||||
"activity_starting": self._activity_update,
|
"activity_starting": self._async_activity_update,
|
||||||
"activity_started": self._activity_update,
|
"activity_started": self._async_activity_update,
|
||||||
"config_updated": None,
|
"config_updated": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.async_on_remove(self._data.async_subscribe(HarmonyCallback(**callbacks)))
|
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()
|
self.async_write_ha_state()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue