Fix use of async in Sonos switch (#51210)

* Fix use of async in Sonos switch

* Simplify

* Convert to callback
This commit is contained in:
jjlawren 2021-05-28 23:28:07 -05:00 committed by GitHub
parent 84f0d3f961
commit bd34059c11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,7 @@ from pysonos.exceptions import SoCoUPnPException
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity
from homeassistant.const import ATTR_TIME 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 import device_registry as dr, entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -99,7 +100,8 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity):
str(self.alarm.start_time)[0:5], 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.""" """Check if alarm exists and remove alarm entity if not available."""
if self.alarm_id in self.hass.data[DATA_SONOS].alarms: if self.alarm_id in self.hass.data[DATA_SONOS].alarms:
return True return True
@ -114,11 +116,9 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity):
async def async_update(self) -> None: async def async_update(self) -> None:
"""Poll the device for the current state.""" """Poll the device for the current state."""
if await self.async_check_if_available(): if not self.async_check_if_available():
await self.hass.async_add_executor_job(self.update_alarm) return
def update_alarm(self):
"""Update the state of the alarm."""
_LOGGER.debug("Updating alarm: %s", self.entity_id) _LOGGER.debug("Updating alarm: %s", self.entity_id)
if self.speaker.soco.uid != self.alarm.zone.uid: if self.speaker.soco.uid != self.alarm.zone.uid:
self.speaker = self.hass.data[DATA_SONOS].discovered.get( 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." "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.""" """Update the device, since this alarm moved to a different player."""
device_registry = dr.async_get(self.hass) device_registry = dr.async_get(self.hass)
entity_registry = er.async_get(self.hass) entity_registry = er.async_get(self.hass)