From 5f728b955e416332d98bb9f87550d84e9f1f979a Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sun, 10 Jul 2022 22:19:13 +0200 Subject: [PATCH] Migrate Sonos to new entity naming style (#74909) --- .../components/sonos/binary_sensor.py | 4 ++-- homeassistant/components/sonos/entity.py | 1 + .../components/sonos/media_player.py | 1 - homeassistant/components/sonos/number.py | 3 +-- homeassistant/components/sonos/sensor.py | 6 +++--- homeassistant/components/sonos/switch.py | 20 ++++++++----------- 6 files changed, 15 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/sonos/binary_sensor.py b/homeassistant/components/sonos/binary_sensor.py index 4eaa75f92ae..e890c1c64a8 100644 --- a/homeassistant/components/sonos/binary_sensor.py +++ b/homeassistant/components/sonos/binary_sensor.py @@ -58,12 +58,12 @@ class SonosPowerEntity(SonosEntity, BinarySensorEntity): _attr_entity_category = EntityCategory.DIAGNOSTIC _attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING + _attr_name = "Power" 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_fallback_poll(self) -> None: """Poll the device for the current state.""" @@ -92,12 +92,12 @@ class SonosMicrophoneSensorEntity(SonosEntity, BinarySensorEntity): _attr_entity_category = EntityCategory.DIAGNOSTIC _attr_icon = "mdi:microphone" + _attr_name = "Microphone" def __init__(self, speaker: SonosSpeaker) -> None: """Initialize the microphone binary sensor entity.""" super().__init__(speaker) self._attr_unique_id = f"{self.soco.uid}-microphone" - self._attr_name = f"{self.speaker.zone_name} Microphone" async def _async_fallback_poll(self) -> None: """Handle polling when subscription fails.""" diff --git a/homeassistant/components/sonos/entity.py b/homeassistant/components/sonos/entity.py index ba1f72cd56b..0955fb0e82d 100644 --- a/homeassistant/components/sonos/entity.py +++ b/homeassistant/components/sonos/entity.py @@ -26,6 +26,7 @@ class SonosEntity(Entity): """Representation of a Sonos entity.""" _attr_should_poll = False + _attr_has_entity_name = True def __init__(self, speaker: SonosSpeaker) -> None: """Initialize a SonosEntity.""" diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index 4e7998c8e4e..1f57cafbf09 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -216,7 +216,6 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity): """Initialize the media player entity.""" super().__init__(speaker) self._attr_unique_id = self.soco.uid - self._attr_name = self.speaker.zone_name async def async_added_to_hass(self) -> None: """Handle common setup when added to hass.""" diff --git a/homeassistant/components/sonos/number.py b/homeassistant/components/sonos/number.py index 3b034423471..ccbcbc3c339 100644 --- a/homeassistant/components/sonos/number.py +++ b/homeassistant/components/sonos/number.py @@ -72,8 +72,7 @@ class SonosLevelEntity(SonosEntity, NumberEntity): """Initialize the level entity.""" super().__init__(speaker) self._attr_unique_id = f"{self.soco.uid}-{level_type}" - name_suffix = level_type.replace("_", " ").title() - self._attr_name = f"{self.speaker.zone_name} {name_suffix}" + self._attr_name = level_type.replace("_", " ").capitalize() self.level_type = level_type self._attr_native_min_value, self._attr_native_max_value = valid_range diff --git a/homeassistant/components/sonos/sensor.py b/homeassistant/components/sonos/sensor.py index 380d1a3b9b6..8477e523a40 100644 --- a/homeassistant/components/sonos/sensor.py +++ b/homeassistant/components/sonos/sensor.py @@ -80,13 +80,13 @@ class SonosBatteryEntity(SonosEntity, SensorEntity): _attr_device_class = SensorDeviceClass.BATTERY _attr_entity_category = EntityCategory.DIAGNOSTIC + _attr_name = "Battery" _attr_native_unit_of_measurement = 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_fallback_poll(self) -> None: """Poll the device for the current state.""" @@ -108,13 +108,13 @@ class SonosAudioInputFormatSensorEntity(SonosPollingEntity, SensorEntity): _attr_entity_category = EntityCategory.DIAGNOSTIC _attr_icon = "mdi:import" + _attr_name = "Audio input format" _attr_should_poll = True def __init__(self, speaker: SonosSpeaker, audio_format: str) -> None: """Initialize the audio input format sensor.""" super().__init__(speaker) self._attr_unique_id = f"{self.soco.uid}-audio-format" - self._attr_name = f"{self.speaker.zone_name} Audio Input Format" self._attr_native_value = audio_format def poll_state(self) -> None: @@ -137,7 +137,7 @@ class SonosFavoritesEntity(SensorEntity): _attr_entity_registry_enabled_default = False _attr_icon = "mdi:star" - _attr_name = "Sonos Favorites" + _attr_name = "Sonos favorites" _attr_native_unit_of_measurement = "items" _attr_should_poll = False diff --git a/homeassistant/components/sonos/switch.py b/homeassistant/components/sonos/switch.py index 53911d85d3e..8dcf47fd0a2 100644 --- a/homeassistant/components/sonos/switch.py +++ b/homeassistant/components/sonos/switch.py @@ -69,13 +69,13 @@ POLL_REQUIRED = ( FRIENDLY_NAMES = { ATTR_CROSSFADE: "Crossfade", ATTR_LOUDNESS: "Loudness", - ATTR_MUSIC_PLAYBACK_FULL_VOLUME: "Surround Music Full Volume", - ATTR_NIGHT_SOUND: "Night Sound", - ATTR_SPEECH_ENHANCEMENT: "Speech Enhancement", + ATTR_MUSIC_PLAYBACK_FULL_VOLUME: "Surround music full volume", + ATTR_NIGHT_SOUND: "Night sound", + ATTR_SPEECH_ENHANCEMENT: "Speech enhancement", ATTR_STATUS_LIGHT: "Status Light", - ATTR_SUB_ENABLED: "Subwoofer Enabled", - ATTR_SURROUND_ENABLED: "Surround Enabled", - ATTR_TOUCH_CONTROLS: "Touch Controls", + ATTR_SUB_ENABLED: "Subwoofer enabled", + ATTR_SURROUND_ENABLED: "Surround enabled", + ATTR_TOUCH_CONTROLS: "Touch controls", } FEATURE_ICONS = { @@ -160,7 +160,7 @@ class SonosSwitchEntity(SonosPollingEntity, SwitchEntity): self.feature_type = feature_type self.needs_coordinator = feature_type in COORDINATOR_FEATURES self._attr_entity_category = EntityCategory.CONFIG - self._attr_name = f"{speaker.zone_name} {FRIENDLY_NAMES[feature_type]}" + self._attr_name = FRIENDLY_NAMES[feature_type] self._attr_unique_id = f"{speaker.soco.uid}-{feature_type}" self._attr_icon = FEATURE_ICONS.get(feature_type) @@ -240,11 +240,7 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity): @property def name(self) -> str: """Return the name of the sensor.""" - return "{} {} Alarm {}".format( - self.speaker.zone_name, - self.alarm.recurrence.title(), - str(self.alarm.start_time)[0:5], - ) + return f"{self.alarm.recurrence.capitalize()} alarm {str(self.alarm.start_time)[:5]}" async def _async_fallback_poll(self) -> None: """Call the central alarm polling method."""