def _discovery(now=None): """Discover players from network or configuration.""" hosts = config.get(CONF_HOSTS) def _discovered_player(soco): """Handle a (re)discovered player.""" try: entity = _get_entity_from_soco_uid(hass, soco.uid) if not entity: hass.add_job(async_add_entities, [SonosEntity(soco)]) else: hass.add_job(entity.async_seen()) except SoCoException: pass if hosts: for host in hosts: try: player = pysonos.SoCo(socket.gethostbyname(host)) if player.is_visible: # Make sure that the player is available _ = player.volume _discovered_player(player) except (OSError, SoCoException): if now is None: _LOGGER.warning("Failed to initialize '%s'", host) hass.helpers.event.call_later(DISCOVERY_INTERVAL, _discovery) else: pysonos.discover_thread( _discovered_player, interval=DISCOVERY_INTERVAL, interface_addr=config.get(CONF_INTERFACE_ADDR))
def _discovery(now=None): """Discover players from network or configuration.""" hosts = config.get(CONF_HOSTS) _LOGGER.debug(hosts) alarm_list = [] def _discovered_alarm(soco): """Handle a (re)discovered player.""" try: _LOGGER.debug("Reached _discovered_player, soco=%s", soco) for one_alarm in alarms.get_alarms(soco): if one_alarm.zone == soco and one_alarm not in alarm_list: _LOGGER.debug("Adding new alarm") alarm_list.append(one_alarm) hass.add_job( async_add_entities, [SonosAlarmSwitch(soco, one_alarm)], ) except SoCoException as ex: _LOGGER.debug("SoCoException, ex=%s", ex) if hosts: for host in hosts: try: _LOGGER.debug("Testing %s", host) player = pysonos.SoCo(socket.gethostbyname(host)) if player.is_visible: # Make sure that the player is available _ = player.volume _discovered_alarm(player) except (OSError, SoCoException) as ex: _LOGGER.debug("Exception %s", ex) if now is None: _LOGGER.warning("Failed to initialize '%s'", host) _LOGGER.debug("Tested all hosts") hass.helpers.event.call_later(DISCOVERY_INTERVAL, _discovery) else: _LOGGER.debug("Starting discovery thread") pysonos.discover_thread( _discovered_alarm, interval=DISCOVERY_INTERVAL, interface_addr=config.get(CONF_INTERFACE_ADDR), )
def _discovery(now=None): """Discover players from network or configuration.""" hosts = config.get(CONF_HOSTS) def _discovered_player(soco): """Handle a (re)discovered player.""" try: _LOGGER.debug("Reached _discovered_player, soco=%s", soco) if soco not in hass.data[DATA_SONOS].discovered: _LOGGER.debug("Adding new entity") hass.data[DATA_SONOS].discovered.append(soco) hass.add_job(async_add_entities, [SonosEntity(soco)]) else: entity = _get_entity_from_soco_uid(hass, soco.uid) if entity: _LOGGER.debug("Seen %s", entity) hass.add_job(entity.async_seen()) except SoCoException as ex: _LOGGER.debug("SoCoException, ex=%s", ex) if hosts: for host in hosts: try: _LOGGER.debug("Testing %s", host) player = pysonos.SoCo(socket.gethostbyname(host)) if player.is_visible: # Make sure that the player is available _ = player.volume _discovered_player(player) except (OSError, SoCoException) as ex: _LOGGER.debug("Exception %s", ex) if now is None: _LOGGER.warning("Failed to initialize '%s'", host) _LOGGER.debug("Tested all hosts") hass.helpers.event.call_later(DISCOVERY_INTERVAL, _discovery) else: _LOGGER.debug("Starting discovery thread") pysonos.discover_thread( _discovered_player, interval=DISCOVERY_INTERVAL, interface_addr=config.get(CONF_INTERFACE_ADDR), )
def _discovery(now: datetime.datetime | None = None) -> None: """Discover players from network or configuration.""" hosts = config.get(CONF_HOSTS) def _discovered_player(soco: SoCo) -> None: """Handle a (re)discovered player.""" try: _LOGGER.debug("Reached _discovered_player, soco=%s", soco) data = hass.data[DATA_SONOS] if soco.uid not in data.discovered: speaker_info = soco.get_speaker_info(True) _LOGGER.debug("Adding new speaker: %s", speaker_info) speaker = SonosSpeaker(hass, soco, speaker_info) data.discovered[soco.uid] = speaker if soco.household_id not in data.favorites: data.favorites[soco.household_id] = SonosFavorites( hass, soco.household_id ) data.favorites[soco.household_id].update() speaker.setup() else: dispatcher_send(hass, f"{SONOS_SEEN}-{soco.uid}", soco) except SoCoException as ex: _LOGGER.debug("SoCoException, ex=%s", ex) if hosts: for host in hosts: try: _LOGGER.debug("Testing %s", host) player = pysonos.SoCo(socket.gethostbyname(host)) if player.is_visible: # Make sure that the player is available _ = player.volume _discovered_player(player) except (OSError, SoCoException) as ex: _LOGGER.debug("Exception %s", ex) if now is None: _LOGGER.warning("Failed to initialize '%s'", host) _LOGGER.debug("Tested all hosts") hass.data[DATA_SONOS].hosts_heartbeat = hass.helpers.event.call_later( DISCOVERY_INTERVAL.total_seconds(), _discovery ) else: _LOGGER.debug("Starting discovery thread") hass.data[DATA_SONOS].discovery_thread = pysonos.discover_thread( _discovered_player, interval=DISCOVERY_INTERVAL.total_seconds(), interface_addr=config.get(CONF_INTERFACE_ADDR), ) hass.data[DATA_SONOS].discovery_thread.name = "Sonos-Discovery"
def _discovery(now=None): """Discover players from network or configuration.""" hosts = config.get(CONF_HOSTS) _LOGGER.debug(hosts) def _discovered_alarm(soco): """Handle a (re)discovered player.""" try: _LOGGER.debug("Reached _discovered_player, soco=%s", soco) for one_alarm in alarms.get_alarms(soco): if one_alarm.zone == soco and one_alarm._alarm_id not in hass.data[DATA_SONOS].discovered: _LOGGER.debug("Adding new alarm") hass.data[DATA_SONOS].discovered.append(one_alarm._alarm_id) hass.add_job( async_add_entities, [SonosAlarmSwitch(one_alarm)], ) else: entity = _get_entity_from_alarm_id(hass, one_alarm._alarm_id) if entity and (entity.soco == soco or not entity.available): _LOGGER.debug("Seen %s", entity) hass.add_job(entity.async_seen(soco)) # type: ignore except SoCoUPnPException as ex: _LOGGER.debug("SoCoException, ex=%s", ex) if hosts: for host in hosts: try: _LOGGER.debug("Testing %s", host) player = pysonos.SoCo(socket.gethostbyname(host)) if player.is_visible: # Make sure that the player is available _ = player.volume _discovered_alarm(player) except (OSError, SoCoUPnPException) as ex: _LOGGER.debug("Exception %s", ex) if now is None: _LOGGER.warning("Failed to initialize '%s'", host) _LOGGER.debug("Tested all hosts") hass.data[DATA_SONOS].hosts_heartbeat = hass.helpers.event.call_later( DISCOVERY_INTERVAL, _discovery) else: _LOGGER.debug("Starting discovery thread") hass.data[DATA_SONOS].discovery_thread = pysonos.discover_thread( _discovered_alarm, interval=DISCOVERY_INTERVAL, interface_addr=config.get(CONF_INTERFACE_ADDR), ) hass.data[DATA_SONOS].discovery_thread.name = "Sonos-Discovery"