mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix use of async in Sonos switch (#51210)
* Fix use of async in Sonos switch * Simplify * Convert to callback
This commit is contained in:
parent
84f0d3f961
commit
bd34059c11
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user