Use _attrs where possible in Sonos (#60931)

This commit is contained in:
jjlawren 2021-12-03 12:57:19 -06:00 committed by GitHub
parent ba99dc3af9
commit 171b57bf32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 74 deletions

View file

@ -33,21 +33,13 @@ class SonosPowerEntity(SonosEntity, BinarySensorEntity):
"""Representation of a Sonos power entity."""
_attr_entity_category = ENTITY_CATEGORY_DIAGNOSTIC
_attr_device_class = DEVICE_CLASS_BATTERY_CHARGING
@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
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"
async def _async_poll(self) -> None:
"""Poll the device for the current state."""

View file

@ -238,25 +238,24 @@ async def async_setup_entry(
class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
"""Representation of a Sonos entity."""
_attr_supported_features = SUPPORT_SONOS
_attr_media_content_type = MEDIA_TYPE_MUSIC
def __init__(self, speaker: SonosSpeaker) -> None:
"""Initialize the media player entity."""
super().__init__(speaker)
self._attr_unique_id = self.soco.uid
self._attr_name = self.speaker.zone_name
@property
def coordinator(self) -> SonosSpeaker:
"""Return the current coordinator SonosSpeaker."""
return self.speaker.coordinator or self.speaker
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self.soco.uid # type: ignore[no-any-return]
def __hash__(self) -> int:
"""Return a hash of self."""
return hash(self.unique_id)
@property
def name(self) -> str:
"""Return the name of the entity."""
return self.speaker.zone_name # type: ignore[no-any-return]
@property # type: ignore[misc]
def state(self) -> str:
"""Return the state of the entity."""
@ -322,11 +321,6 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
"""Content id of current playing media."""
return self.media.uri
@property
def media_content_type(self) -> str:
"""Content type of current playing media."""
return MEDIA_TYPE_MUSIC
@property # type: ignore[misc]
def media_duration(self) -> float | None:
"""Duration of current playing media in seconds."""
@ -377,11 +371,6 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
"""Name of the current input source."""
return self.media.source_name or None
@property # type: ignore[misc]
def supported_features(self) -> int:
"""Flag media player features that are supported."""
return SUPPORT_SONOS
@soco_error()
def volume_up(self) -> None:
"""Volume up media player."""

View file

@ -39,18 +39,10 @@ class SonosLevelEntity(SonosEntity, NumberEntity):
def __init__(self, speaker: SonosSpeaker, level_type: str) -> None:
"""Initialize the level entity."""
super().__init__(speaker)
self._attr_unique_id = f"{self.soco.uid}-{level_type}"
self._attr_name = f"{self.speaker.zone_name} {level_type.capitalize()}"
self.level_type = level_type
@property
def unique_id(self) -> str:
"""Return the unique ID."""
return f"{self.soco.uid}-{self.level_type}"
@property
def name(self) -> str:
"""Return the name."""
return f"{self.speaker.zone_name} {self.level_type.capitalize()}"
async def _async_poll(self) -> None:
"""Poll the value if subscriptions are not working."""
# Handled by SonosSpeaker

View file

@ -45,27 +45,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class SonosBatteryEntity(SonosEntity, SensorEntity):
"""Representation of a Sonos Battery entity."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_entity_category = ENTITY_CATEGORY_DIAGNOSTIC
_attr_native_unit_of_measurement = PERCENTAGE
@property
def unique_id(self) -> str:
"""Return the unique ID of the sensor."""
return f"{self.soco.uid}-battery"
@property
def name(self) -> str:
"""Return the name of the sensor."""
return f"{self.speaker.zone_name} Battery"
@property
def device_class(self) -> str:
"""Return the entity's device class."""
return DEVICE_CLASS_BATTERY
@property
def native_unit_of_measurement(self) -> str:
"""Get the unit of measurement."""
return PERCENTAGE
def __init__(self, speaker: SonosSpeaker) -> None:
"""Initialize the battery sensor."""
super().__init__(speaker)
self._attr_unique_id = f"{self.soco.uid}-battery"
self._attr_name = f"{self.speaker.zone_name} Battery"
async def _async_poll(self) -> None:
"""Poll the device for the current state."""

View file

@ -195,11 +195,12 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity):
"""Representation of a Sonos Alarm entity."""
_attr_entity_category = ENTITY_CATEGORY_CONFIG
_attr_icon = "mdi:alarm"
def __init__(self, alarm_id: str, speaker: SonosSpeaker) -> None:
"""Initialize the switch."""
super().__init__(speaker)
self._attr_unique_id = f"{SONOS_DOMAIN}-{alarm_id}"
self.alarm_id = alarm_id
self.household_id = speaker.household_id
self.entity_id = ENTITY_ID_FORMAT.format(f"sonos_alarm_{self.alarm_id}")
@ -220,16 +221,6 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity):
"""Return the alarm instance."""
return self.hass.data[DATA_SONOS].alarms[self.household_id].get(self.alarm_id)
@property
def unique_id(self) -> str:
"""Return the unique ID of the switch."""
return f"{SONOS_DOMAIN}-{self.alarm_id}"
@property
def icon(self):
"""Return icon of Sonos alarm switch."""
return "mdi:alarm"
@property
def name(self) -> str:
"""Return the name of the sensor."""