Cleanup implementation of new Sonos sensors (#49716)
This commit is contained in:
parent
9e7d83b2d5
commit
dc50524f32
6 changed files with 35 additions and 47 deletions
|
@ -10,14 +10,12 @@ from pysonos.core import SoCo
|
|||
from pysonos.events_base import Event as SonosEvent
|
||||
from pysonos.exceptions import SoCoException
|
||||
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorEntity
|
||||
from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE, STATE_UNKNOWN
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
async_dispatcher_connect,
|
||||
async_dispatcher_send,
|
||||
)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.icon import icon_for_battery_level
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import SonosData
|
||||
|
@ -51,6 +49,7 @@ def fetch_battery_info_or_none(soco: SoCo) -> dict[str, Any] | None:
|
|||
"""
|
||||
with contextlib.suppress(ConnectionError, TimeoutError, SoCoException):
|
||||
return soco.get_battery_info()
|
||||
return None
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
|
@ -76,16 +75,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
async_dispatcher_connect(hass, SONOS_DISCOVERY_UPDATE, _async_create_entities)
|
||||
|
||||
|
||||
class SonosBatteryEntity(SonosEntity, Entity):
|
||||
class SonosBatteryEntity(SonosEntity, SensorEntity):
|
||||
"""Representation of a Sonos Battery entity."""
|
||||
|
||||
def __init__(
|
||||
self, speaker: SonosSpeaker, sonos_data: SonosData, battery_info: dict[str, Any]
|
||||
):
|
||||
) -> None:
|
||||
"""Initialize a SonosBatteryEntity."""
|
||||
super().__init__(speaker, sonos_data)
|
||||
self._battery_info: dict[str, Any] = battery_info
|
||||
self._last_event: datetime.datetime = None
|
||||
self._last_event: datetime.datetime | None = None
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Register polling callback when added to hass."""
|
||||
|
@ -185,11 +184,6 @@ class SonosBatteryEntity(SonosEntity, Entity):
|
|||
"""Return the charging status of this battery."""
|
||||
return self.power_source not in ("BATTERY", STATE_UNKNOWN)
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon of the sensor."""
|
||||
return icon_for_battery_level(self.battery_level, self.charging)
|
||||
|
||||
@property
|
||||
def state(self) -> int | None:
|
||||
"""Return the state of the sensor."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue