diff --git a/custom_components/sonos_alarm/switch.py b/custom_components/sonos_alarm/switch.py index 56f615e..31ac3da 100644 --- a/custom_components/sonos_alarm/switch.py +++ b/custom_components/sonos_alarm/switch.py @@ -152,29 +152,34 @@ def _get_current_alarm_instance(self): else: return False - async def _async_remove(self): - """remove this entity as it is not available anymore""" - entity_registry = er.async_get(self.hass) - entity_registry.async_remove(self.entity_id) - - async 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) - entry_id = entity_registry.async_get(self.entity_id).config_entry_id - - new_device = device_registry.async_get_or_create(config_entry_id=entry_id, identifiers = {(SONOS_DOMAIN, self._unique_player_id)}, connections={(dr.CONNECTION_NETWORK_MAC, self._mac_address)}) - if not entity_registry.async_get(self.entity_id).device_id == new_device.id: - entity_registry._async_update_entity(self.entity_id, device_id=new_device.id) - async def async_update(self, now=None): """Retrieve latest state.""" _LOGGER.debug("updating alarms") + + async def _async_remove(): + """remove this entity as it is not available anymore""" + entity_registry = er.async_get(self.hass) + entity_registry.async_remove(self.entity_id) + + def _update_device(): + """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) + entry_id = entity_registry.async_get(self.entity_id).config_entry_id + + new_device = device_registry.async_get_or_create(config_entry_id=entry_id, + identifiers={(SONOS_DOMAIN, self._unique_player_id)}, + connections={ + (dr.CONNECTION_NETWORK_MAC, self._mac_address)}) + if not entity_registry.async_get(self.entity_id).device_id == new_device.id: + entity_registry._async_update_entity(self.entity_id, device_id=new_device.id) + + try: - self._is_available = await self.hass.async_add_executor_job(self._get_current_alarm_instance) + is_available = await self.hass.async_add_executor_job(self._get_current_alarm_instance) - if not self._is_available: - self.hass.async_create_task(self._async_remove()) + if not is_available: + self.hass.async_create_task(_async_remove()) return self._is_on = self.alarm.enabled @@ -186,7 +191,7 @@ async def async_update(self, now=None): self._sw_version: str = speaker_info["software_version"] self._mac_address: str = speaker_info["mac_address"] self._unique_player_id: str = self.alarm.zone.uid - await self._update_device() + _update_device() self._name = "Sonos Alarm {} {} {}".format(