1 """Entity representing a Sonos power sensor."""
3 from __future__
import annotations
9 BinarySensorDeviceClass,
18 from .const
import SONOS_CREATE_BATTERY, SONOS_CREATE_MIC_SENSOR
19 from .entity
import SonosEntity
20 from .helpers
import soco_error
21 from .speaker
import SonosSpeaker
23 ATTR_BATTERY_POWER_SOURCE =
"power_source"
25 _LOGGER = logging.getLogger(__name__)
30 config_entry: ConfigEntry,
31 async_add_entities: AddEntitiesCallback,
33 """Set up Sonos from a config entry."""
36 def _async_create_battery_entity(speaker: SonosSpeaker) ->
None:
37 _LOGGER.debug(
"Creating battery binary_sensor on %s", speaker.zone_name)
42 def _async_create_mic_entity(speaker: SonosSpeaker) ->
None:
43 _LOGGER.debug(
"Creating microphone binary_sensor on %s", speaker.zone_name)
46 config_entry.async_on_unload(
48 hass, SONOS_CREATE_BATTERY, _async_create_battery_entity
52 config_entry.async_on_unload(
54 hass, SONOS_CREATE_MIC_SENSOR, _async_create_mic_entity
60 """Representation of a Sonos power entity."""
62 _attr_entity_category = EntityCategory.DIAGNOSTIC
63 _attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
65 def __init__(self, speaker: SonosSpeaker) ->
None:
66 """Initialize the power entity binary sensor."""
71 """Poll the device for the current state."""
72 await self.
speakerspeaker.async_poll_battery()
76 """Return the state of the binary sensor."""
77 return self.
speakerspeaker.charging
81 """Return entity specific state attributes."""
83 ATTR_BATTERY_POWER_SOURCE: self.
speakerspeaker.power_source,
88 """Return whether this device is available."""
89 return self.
speakerspeaker.available
and (self.
speakerspeaker.charging
is not None)
93 """Representation of a Sonos microphone sensor entity."""
95 _attr_entity_category = EntityCategory.DIAGNOSTIC
96 _attr_translation_key =
"microphone"
98 def __init__(self, speaker: SonosSpeaker) ->
None:
99 """Initialize the microphone binary sensor entity."""
104 """Handle polling when subscription fails."""
105 await self.
hasshass.async_add_executor_job(self.
poll_statepoll_state)
109 """Poll the current state of the microphone."""
110 self.
speakerspeaker.mic_enabled = self.
socosoco.mic_enabled
114 """Return the state of the binary sensor."""
115 return self.
speakerspeaker.mic_enabled
None _async_fallback_poll(self)
None __init__(self, SonosSpeaker speaker)
None _async_fallback_poll(self)
dict[str, Any] extra_state_attributes(self)
None __init__(self, SonosSpeaker speaker)
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)