1 """Entity representing a Sonos battery level."""
3 from __future__
import annotations
15 SONOS_CREATE_AUDIO_FORMAT_SENSOR,
17 SONOS_CREATE_FAVORITES_SENSOR,
18 SONOS_FAVORITES_UPDATED,
21 from .entity
import SonosEntity, SonosPollingEntity
22 from .favorites
import SonosFavorites
23 from .helpers
import soco_error
24 from .speaker
import SonosSpeaker
26 _LOGGER = logging.getLogger(__name__)
31 config_entry: ConfigEntry,
32 async_add_entities: AddEntitiesCallback,
34 """Set up Sonos from a config entry."""
37 def _async_create_audio_format_entity(
38 speaker: SonosSpeaker, audio_format: str
40 _LOGGER.debug(
"Creating audio input format sensor on %s", speaker.zone_name)
45 def _async_create_battery_sensor(speaker: SonosSpeaker) ->
None:
46 _LOGGER.debug(
"Creating battery level sensor on %s", speaker.zone_name)
51 def _async_create_favorites_sensor(favorites: SonosFavorites) ->
None:
53 "Creating favorites sensor (%s items) for household %s",
55 favorites.household_id,
60 config_entry.async_on_unload(
62 hass, SONOS_CREATE_AUDIO_FORMAT_SENSOR, _async_create_audio_format_entity
65 config_entry.async_on_unload(
67 hass, SONOS_CREATE_BATTERY, _async_create_battery_sensor
71 config_entry.async_on_unload(
73 hass, SONOS_CREATE_FAVORITES_SENSOR, _async_create_favorites_sensor
79 """Representation of a Sonos Battery entity."""
81 _attr_device_class = SensorDeviceClass.BATTERY
82 _attr_entity_category = EntityCategory.DIAGNOSTIC
83 _attr_native_unit_of_measurement = PERCENTAGE
85 def __init__(self, speaker: SonosSpeaker) ->
None:
86 """Initialize the battery sensor."""
91 """Poll the device for the current state."""
92 await self.
speakerspeaker.async_poll_battery()
96 """Return the state of the sensor."""
97 return self.
speakerspeaker.battery_info.get(
"Level")
101 """Return whether this device is available."""
102 return self.
speakerspeaker.available
and self.
speakerspeaker.power_source
is not None
106 """Representation of a Sonos audio import format sensor entity."""
108 _attr_entity_category = EntityCategory.DIAGNOSTIC
109 _attr_translation_key =
"audio_input_format"
110 _attr_should_poll =
True
112 def __init__(self, speaker: SonosSpeaker, audio_format: str) ->
None:
113 """Initialize the audio input format sensor."""
119 """Poll the state if TV source is active and state has settled."""
126 """Poll the device for the current state."""
130 """Provide a stub for required ABC method."""
133 class SonosFavoritesEntity(SensorEntity):
134 """Representation of a Sonos favorites info entity."""
136 _attr_entity_registry_enabled_default =
False
137 _attr_name =
"Sonos favorites"
138 _attr_translation_key =
"favorites"
139 _attr_native_unit_of_measurement =
"items"
140 _attr_should_poll =
False
142 def __init__(self, favorites: SonosFavorites) ->
None:
143 """Initialize the favorites sensor."""
148 """Handle common setup when added to hass."""
153 f
"{SONOS_FAVORITES_UPDATED}-{self.favorites.household_id}",
161 "items": {fav.item_id: fav.title
for fav
in self.
favoritesfavorites}
None _async_fallback_poll(self)
None __init__(self, SonosSpeaker speaker)
int|None native_value(self)
None _async_update_state(self)
None __init__(self, SonosFavorites favorites)
None async_added_to_hass(self)
_attr_extra_state_attributes
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)