Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak Sonos activity monitoring #66207

Merged
merged 2 commits into from Feb 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 23 additions & 16 deletions homeassistant/components/sonos/speaker.py
Expand Up @@ -79,6 +79,7 @@
"renderingControl",
"zoneGroupTopology",
]
SUPPORTED_VANISH_REASONS = ("sleeping", "upgrade")
UNAVAILABLE_VALUES = {"", "NOT_IMPLEMENTED", None}
UNUSED_DEVICE_KEYS = ["SPID", "TargetRoomName"]

Expand Down Expand Up @@ -553,25 +554,29 @@ def speaker_activity(self, source):

async def async_check_activity(self, now: datetime.datetime) -> None:
"""Validate availability of the speaker based on recent activity."""
if not self.available:
return
if time.monotonic() - self._last_activity < AVAILABILITY_TIMEOUT:
return

try:
_ = await self.hass.async_add_executor_job(getattr, self.soco, "volume")
except (OSError, SoCoException):
pass
# Make a short-timeout call as a final check
# before marking this speaker as unavailable
await self.hass.async_add_executor_job(
partial(
self.soco.renderingControl.GetVolume,
[("InstanceID", 0), ("Channel", "Master")],
timeout=1,
)
)
except OSError:
_LOGGER.warning(
Kane610 marked this conversation as resolved.
Show resolved Hide resolved
"No recent activity and cannot reach %s, marking unavailable",
self.zone_name,
)
await self.async_offline()
else:
self.speaker_activity("timeout poll")
return

if not self.available:
return

_LOGGER.warning(
"No recent activity and cannot reach %s, marking unavailable",
self.zone_name,
)
await self.async_offline()

async def async_offline(self) -> None:
"""Handle removal of speaker when unavailable."""
Expand Down Expand Up @@ -603,8 +608,8 @@ async def async_vanished(self, reason: str) -> None:

async def async_rebooted(self, soco: SoCo) -> None:
"""Handle a detected speaker reboot."""
_LOGGER.warning(
"%s rebooted or lost network connectivity, reconnecting with %s",
_LOGGER.debug(
"%s rebooted, reconnecting with %s",
self.zone_name,
soco,
)
Expand Down Expand Up @@ -717,7 +722,9 @@ def async_update_groups(self, event: SonosEvent) -> None:
if xml := event.variables.get("zone_group_state"):
zgs = ET.fromstring(xml)
for vanished_device in zgs.find("VanishedDevices") or []:
if (reason := vanished_device.get("Reason")) != "sleeping":
if (
reason := vanished_device.get("Reason")
) not in SUPPORTED_VANISH_REASONS:
_LOGGER.debug(
"Ignoring %s marked %s as vanished with reason: %s",
self.zone_name,
Expand Down