From 171b57bf3210da9ec9a44b102e497d41efd0670b Mon Sep 17 00:00:00 2001 From: jjlawren Date: Fri, 3 Dec 2021 12:57:19 -0600 Subject: [PATCH] Use _attrs where possible in Sonos (#60931) --- .../components/sonos/binary_sensor.py | 20 ++++--------- .../components/sonos/media_player.py | 29 ++++++------------- homeassistant/components/sonos/number.py | 12 ++------ homeassistant/components/sonos/sensor.py | 26 +++++------------ homeassistant/components/sonos/switch.py | 13 ++------- 5 files changed, 26 insertions(+), 74 deletions(-) diff --git a/homeassistant/components/sonos/binary_sensor.py b/homeassistant/components/sonos/binary_sensor.py index 488f29a7be8..615ad24e655 100644 --- a/homeassistant/components/sonos/binary_sensor.py +++ b/homeassistant/components/sonos/binary_sensor.py @@ -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.""" diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index 664245a1c99..90c33d7a4e6 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -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.""" diff --git a/homeassistant/components/sonos/number.py b/homeassistant/components/sonos/number.py index 574eba95137..2bcfe5cd5ec 100644 --- a/homeassistant/components/sonos/number.py +++ b/homeassistant/components/sonos/number.py @@ -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 diff --git a/homeassistant/components/sonos/sensor.py b/homeassistant/components/sonos/sensor.py index 7c9235cf4af..62017f4d541 100644 --- a/homeassistant/components/sonos/sensor.py +++ b/homeassistant/components/sonos/sensor.py @@ -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.""" diff --git a/homeassistant/components/sonos/switch.py b/homeassistant/components/sonos/switch.py index f6fe81953f5..e92263991ab 100644 --- a/homeassistant/components/sonos/switch.py +++ b/homeassistant/components/sonos/switch.py @@ -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."""