Refactor Sonos polling (#65722)

* Refactor Sonos polling

Explicitly rename fallback polling
Catch soco exceptions centrally where possible
Create SonosPollingEntity subclass
Remove unnecessary soco_error fixture argument
Remove unnecessary polling in update_volume()
Adjust log levels and wording
Set explicit timeout on library

* Adjust logging to use raised exceptions

* Simplify availabiliity checks when using built-in poller

* Fix typing for return values
This commit is contained in:
jjlawren 2022-02-08 12:17:05 -06:00 committed by GitHub
parent 4efebcb86c
commit a7fd477c64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 89 additions and 85 deletions

View file

@ -12,7 +12,8 @@ from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import SONOS_CREATE_AUDIO_FORMAT_SENSOR, SONOS_CREATE_BATTERY
from .entity import SonosEntity
from .entity import SonosEntity, SonosPollingEntity
from .helpers import soco_error
from .speaker import SonosSpeaker
_LOGGER = logging.getLogger(__name__)
@ -64,7 +65,7 @@ class SonosBatteryEntity(SonosEntity, SensorEntity):
self._attr_unique_id = f"{self.soco.uid}-battery"
self._attr_name = f"{self.speaker.zone_name} Battery"
async def _async_poll(self) -> None:
async def _async_fallback_poll(self) -> None:
"""Poll the device for the current state."""
await self.speaker.async_poll_battery()
@ -79,7 +80,7 @@ class SonosBatteryEntity(SonosEntity, SensorEntity):
return self.speaker.available and self.speaker.power_source
class SonosAudioInputFormatSensorEntity(SonosEntity, SensorEntity):
class SonosAudioInputFormatSensorEntity(SonosPollingEntity, SensorEntity):
"""Representation of a Sonos audio import format sensor entity."""
_attr_entity_category = EntityCategory.DIAGNOSTIC
@ -93,9 +94,10 @@ class SonosAudioInputFormatSensorEntity(SonosEntity, SensorEntity):
self._attr_name = f"{self.speaker.zone_name} Audio Input Format"
self._attr_native_value = audio_format
def update(self) -> None:
@soco_error()
def poll_state(self) -> None:
"""Poll the device for the current state."""
self._attr_native_value = self.soco.soundbar_audio_input_format
async def _async_poll(self) -> None:
async def _async_fallback_poll(self) -> None:
"""Provide a stub for required ABC method."""