Skip to content

Commit

Permalink
Merge pull request #27 from AaronDavidSchneider/devices_bug_fix
Browse files Browse the repository at this point in the history
small bug fix
  • Loading branch information
AaronDavidSchneider authored Apr 21, 2021
2 parents d45447f + 2f567cc commit d8fb277
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions custom_components/sonos_alarm/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit d8fb277

Please sign in to comment.