2021-04-27 14:52:05 +00:00
|
|
|
"""Entity representing a Sonos power sensor."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DEVICE_CLASS_BATTERY_CHARGING,
|
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2021-10-24 22:52:15 +00:00
|
|
|
from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC
|
2021-04-27 14:52:05 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
2021-04-30 05:01:09 +00:00
|
|
|
from .const import SONOS_CREATE_BATTERY
|
2021-05-11 17:36:40 +00:00
|
|
|
from .entity import SonosEntity
|
2021-04-27 14:52:05 +00:00
|
|
|
from .speaker import SonosSpeaker
|
|
|
|
|
|
|
|
ATTR_BATTERY_POWER_SOURCE = "power_source"
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Sonos from a config entry."""
|
|
|
|
|
|
|
|
async def _async_create_entity(speaker: SonosSpeaker) -> None:
|
2021-04-30 05:01:09 +00:00
|
|
|
entity = SonosPowerEntity(speaker)
|
2021-04-27 14:52:05 +00:00
|
|
|
async_add_entities([entity])
|
|
|
|
|
|
|
|
config_entry.async_on_unload(
|
|
|
|
async_dispatcher_connect(hass, SONOS_CREATE_BATTERY, _async_create_entity)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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-10-24 22:52:15 +00:00
|
|
|
_attr_entity_category = ENTITY_CATEGORY_DIAGNOSTIC
|
|
|
|
|
2021-04-27 14:52:05 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID of the sensor."""
|
|
|
|
return f"{self.soco.uid}-power"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return f"{self.speaker.zone_name} Power"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self) -> str:
|
|
|
|
"""Return the entity's device class."""
|
|
|
|
return DEVICE_CLASS_BATTERY_CHARGING
|
|
|
|
|
2021-10-23 21:11:27 +00:00
|
|
|
async def _async_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)
|