2021-04-27 14:52:05 +00:00
|
|
|
"""Entity representing a Sonos power sensor."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-12-22 04:36:12 +00:00
|
|
|
import logging
|
2021-04-27 14:52:05 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-17 19:35:21 +00:00
|
|
|
BinarySensorDeviceClass,
|
2021-04-27 14:52:05 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2021-12-27 22:42:24 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-27 14:52:05 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-12-17 19:35:21 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-12-27 22:42:24 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-04-27 14:52:05 +00:00
|
|
|
|
2022-01-04 17:45:40 +00:00
|
|
|
from .const import SONOS_CREATE_BATTERY, SONOS_CREATE_MIC_SENSOR
|
2021-05-11 17:36:40 +00:00
|
|
|
from .entity import SonosEntity
|
2022-02-21 18:46:20 +00:00
|
|
|
from .helpers import soco_error
|
2021-04-27 14:52:05 +00:00
|
|
|
from .speaker import SonosSpeaker
|
|
|
|
|
|
|
|
ATTR_BATTERY_POWER_SOURCE = "power_source"
|
|
|
|
|
2021-12-22 04:36:12 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-04-27 14:52:05 +00:00
|
|
|
|
2021-12-27 22:42:24 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-04-27 14:52:05 +00:00
|
|
|
"""Set up Sonos from a config entry."""
|
|
|
|
|
2022-01-04 17:45:40 +00:00
|
|
|
async def _async_create_battery_entity(speaker: SonosSpeaker) -> None:
|
2021-12-22 04:36:12 +00:00
|
|
|
_LOGGER.debug("Creating battery binary_sensor on %s", speaker.zone_name)
|
2021-04-30 05:01:09 +00:00
|
|
|
entity = SonosPowerEntity(speaker)
|
2021-04-27 14:52:05 +00:00
|
|
|
async_add_entities([entity])
|
|
|
|
|
2022-01-04 17:45:40 +00:00
|
|
|
async def _async_create_mic_entity(speaker: SonosSpeaker) -> None:
|
|
|
|
_LOGGER.debug("Creating microphone binary_sensor on %s", speaker.zone_name)
|
|
|
|
async_add_entities([SonosMicrophoneSensorEntity(speaker)])
|
|
|
|
|
|
|
|
config_entry.async_on_unload(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
hass, SONOS_CREATE_BATTERY, _async_create_battery_entity
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-04-27 14:52:05 +00:00
|
|
|
config_entry.async_on_unload(
|
2022-01-04 17:45:40 +00:00
|
|
|
async_dispatcher_connect(
|
|
|
|
hass, SONOS_CREATE_MIC_SENSOR, _async_create_mic_entity
|
|
|
|
)
|
2021-04-27 14:52:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-11 17:36:40 +00:00
|
|
|
class SonosPowerEntity(SonosEntity, BinarySensorEntity):
|
2021-04-27 14:52:05 +00:00
|
|
|
"""Representation of a Sonos power entity."""
|
|
|
|
|
2021-12-17 19:35:21 +00:00
|
|
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
|
|
|
_attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
|
2021-10-24 22:52:15 +00:00
|
|
|
|
2021-12-03 18:57:19 +00:00
|
|
|
def __init__(self, speaker: SonosSpeaker) -> None:
|
|
|
|
"""Initialize the power entity binary sensor."""
|
|
|
|
super().__init__(speaker)
|
|
|
|
self._attr_unique_id = f"{self.soco.uid}-power"
|
|
|
|
self._attr_name = f"{self.speaker.zone_name} Power"
|
2021-04-27 14:52:05 +00:00
|
|
|
|
2022-02-08 18:17:05 +00:00
|
|
|
async def _async_fallback_poll(self) -> None:
|
2021-04-27 14:52:05 +00:00
|
|
|
"""Poll the device for the current state."""
|
|
|
|
await self.speaker.async_poll_battery()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
"""Return the state of the binary sensor."""
|
|
|
|
return self.speaker.charging
|
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
|
|
|
"""Return entity specific state attributes."""
|
|
|
|
return {
|
|
|
|
ATTR_BATTERY_POWER_SOURCE: self.speaker.power_source,
|
|
|
|
}
|
2021-05-25 16:39:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return whether this device is available."""
|
|
|
|
return self.speaker.available and (self.speaker.charging is not None)
|
2022-01-04 17:45:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SonosMicrophoneSensorEntity(SonosEntity, BinarySensorEntity):
|
|
|
|
"""Representation of a Sonos microphone sensor entity."""
|
|
|
|
|
|
|
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
|
|
|
_attr_icon = "mdi:microphone"
|
|
|
|
|
|
|
|
def __init__(self, speaker: SonosSpeaker) -> None:
|
|
|
|
"""Initialize the microphone binary sensor entity."""
|
|
|
|
super().__init__(speaker)
|
|
|
|
self._attr_unique_id = f"{self.soco.uid}-microphone"
|
|
|
|
self._attr_name = f"{self.speaker.zone_name} Microphone"
|
|
|
|
|
2022-02-08 18:17:05 +00:00
|
|
|
async def _async_fallback_poll(self) -> None:
|
2022-02-21 18:46:20 +00:00
|
|
|
"""Handle polling when subscription fails."""
|
|
|
|
await self.hass.async_add_executor_job(self.poll_state)
|
|
|
|
|
|
|
|
@soco_error()
|
|
|
|
def poll_state(self) -> None:
|
|
|
|
"""Poll the current state of the microphone."""
|
|
|
|
self.speaker.mic_enabled = self.soco.mic_enabled
|
2022-01-04 17:45:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return the state of the binary sensor."""
|
|
|
|
return self.speaker.mic_enabled
|