2021-11-29 15:00:37 +00:00
|
|
|
"""Entity representing a Sonos number control."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-12-22 04:36:12 +00:00
|
|
|
import logging
|
|
|
|
|
2021-11-29 15:00:37 +00:00
|
|
|
from homeassistant.components.number import NumberEntity
|
2021-12-27 22:42:24 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-10 15:04:40 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-11-29 15:00:37 +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-11-29 15:00:37 +00:00
|
|
|
|
|
|
|
from .const import SONOS_CREATE_LEVELS
|
|
|
|
from .entity import SonosEntity
|
2022-01-10 15:04:40 +00:00
|
|
|
from .exception import SpeakerUnavailable
|
2021-11-29 15:00:37 +00:00
|
|
|
from .helpers import soco_error
|
|
|
|
from .speaker import SonosSpeaker
|
|
|
|
|
2022-01-10 15:04:40 +00:00
|
|
|
LEVEL_TYPES = {
|
|
|
|
"audio_delay": (0, 5),
|
|
|
|
"bass": (-10, 10),
|
|
|
|
"treble": (-10, 10),
|
|
|
|
}
|
2021-11-29 15:00:37 +00:00
|
|
|
|
2021-12-22 04:36:12 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-11-29 15:00:37 +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-11-29 15:00:37 +00:00
|
|
|
"""Set up the Sonos number platform from a config entry."""
|
|
|
|
|
2022-01-10 15:04:40 +00:00
|
|
|
def available_soco_attributes(speaker: SonosSpeaker) -> list[str]:
|
|
|
|
features = []
|
|
|
|
for level_type, valid_range in LEVEL_TYPES.items():
|
|
|
|
if (state := getattr(speaker.soco, level_type, None)) is not None:
|
|
|
|
setattr(speaker, level_type, state)
|
|
|
|
features.append((level_type, valid_range))
|
|
|
|
return features
|
|
|
|
|
|
|
|
async def _async_create_entities(speaker: SonosSpeaker) -> None:
|
2021-11-29 15:00:37 +00:00
|
|
|
entities = []
|
2022-01-10 15:04:40 +00:00
|
|
|
|
|
|
|
available_features = await hass.async_add_executor_job(
|
|
|
|
available_soco_attributes, speaker
|
|
|
|
)
|
|
|
|
|
|
|
|
for level_type, valid_range in available_features:
|
2021-12-22 04:36:12 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Creating %s number control on %s", level_type, speaker.zone_name
|
|
|
|
)
|
2022-01-10 15:04:40 +00:00
|
|
|
entities.append(SonosLevelEntity(speaker, level_type, valid_range))
|
2021-11-29 15:00:37 +00:00
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
config_entry.async_on_unload(
|
|
|
|
async_dispatcher_connect(hass, SONOS_CREATE_LEVELS, _async_create_entities)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SonosLevelEntity(SonosEntity, NumberEntity):
|
|
|
|
"""Representation of a Sonos level entity."""
|
|
|
|
|
2021-12-17 19:35:21 +00:00
|
|
|
_attr_entity_category = EntityCategory.CONFIG
|
2021-11-29 15:00:37 +00:00
|
|
|
|
2022-01-10 15:04:40 +00:00
|
|
|
def __init__(
|
|
|
|
self, speaker: SonosSpeaker, level_type: str, valid_range: tuple[int]
|
|
|
|
) -> None:
|
2021-11-29 15:00:37 +00:00
|
|
|
"""Initialize the level entity."""
|
|
|
|
super().__init__(speaker)
|
2021-12-03 18:57:19 +00:00
|
|
|
self._attr_unique_id = f"{self.soco.uid}-{level_type}"
|
2022-01-10 15:04:40 +00:00
|
|
|
name_suffix = level_type.replace("_", " ").title()
|
|
|
|
self._attr_name = f"{self.speaker.zone_name} {name_suffix}"
|
2021-11-29 15:00:37 +00:00
|
|
|
self.level_type = level_type
|
2022-01-10 15:04:40 +00:00
|
|
|
self._attr_min_value, self._attr_max_value = valid_range
|
2021-11-29 15:00:37 +00:00
|
|
|
|
|
|
|
async def _async_poll(self) -> None:
|
|
|
|
"""Poll the value if subscriptions are not working."""
|
2022-01-10 15:04:40 +00:00
|
|
|
await self.hass.async_add_executor_job(self.update)
|
|
|
|
|
|
|
|
@soco_error(raise_on_err=False)
|
|
|
|
def update(self) -> None:
|
|
|
|
"""Fetch number state if necessary."""
|
|
|
|
if not self.available:
|
|
|
|
raise SpeakerUnavailable
|
|
|
|
|
|
|
|
state = getattr(self.soco, self.level_type)
|
|
|
|
setattr(self.speaker, self.level_type, state)
|
2021-11-29 15:00:37 +00:00
|
|
|
|
|
|
|
@soco_error()
|
|
|
|
def set_value(self, value: float) -> None:
|
|
|
|
"""Set a new value."""
|
|
|
|
setattr(self.soco, self.level_type, value)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def value(self) -> float:
|
|
|
|
"""Return the current value."""
|
|
|
|
return getattr(self.speaker, self.level_type)
|