From bd34059c11445efd03200c8fd39313a7b9e9b186 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Fri, 28 May 2021 23:28:07 -0500 Subject: [PATCH] Fix use of async in Sonos switch (#51210) * Fix use of async in Sonos switch * Simplify * Convert to callback --- homeassistant/components/sonos/switch.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/sonos/switch.py b/homeassistant/components/sonos/switch.py index 2038002db2d..4b24224f6a0 100644 --- a/homeassistant/components/sonos/switch.py +++ b/homeassistant/components/sonos/switch.py @@ -8,6 +8,7 @@ from pysonos.exceptions import SoCoUPnPException from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity from homeassistant.const import ATTR_TIME +from homeassistant.core import callback from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -99,7 +100,8 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity): str(self.alarm.start_time)[0:5], ) - async def async_check_if_available(self): + @callback + def async_check_if_available(self): """Check if alarm exists and remove alarm entity if not available.""" if self.alarm_id in self.hass.data[DATA_SONOS].alarms: return True @@ -114,11 +116,9 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity): async def async_update(self) -> None: """Poll the device for the current state.""" - if await self.async_check_if_available(): - await self.hass.async_add_executor_job(self.update_alarm) + if not self.async_check_if_available(): + return - def update_alarm(self): - """Update the state of the alarm.""" _LOGGER.debug("Updating alarm: %s", self.entity_id) if self.speaker.soco.uid != self.alarm.zone.uid: self.speaker = self.hass.data[DATA_SONOS].discovered.get( @@ -129,11 +129,12 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity): "No configured Sonos speaker has been found to match the alarm." ) - self._update_device() + self._async_update_device() - self.schedule_update_ha_state() + self.async_write_ha_state() - def _update_device(self): + @callback + def _async_update_device(self): """Update the device, since this alarm moved to a different player.""" device_registry = dr.async_get(self.hass) entity_registry = er.async_get(self.hass)