From 76c3a638c45bfd0ef2387e2318343f8653fc8fcf Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Tue, 12 Sep 2023 15:17:57 +0200 Subject: [PATCH] Use shorthand attributes in Smarttub (#99839) --- .../components/smarttub/binary_sensor.py | 16 ++-------- homeassistant/components/smarttub/climate.py | 6 +--- homeassistant/components/smarttub/entity.py | 31 ++++++------------- homeassistant/components/smarttub/light.py | 14 ++------- homeassistant/components/smarttub/switch.py | 6 +--- 5 files changed, 17 insertions(+), 56 deletions(-) diff --git a/homeassistant/components/smarttub/binary_sensor.py b/homeassistant/components/smarttub/binary_sensor.py index a1159bcc0ef..99037cd623c 100644 --- a/homeassistant/components/smarttub/binary_sensor.py +++ b/homeassistant/components/smarttub/binary_sensor.py @@ -76,19 +76,13 @@ class SmartTubOnline(SmartTubSensorBase, BinarySensorEntity): """A binary sensor indicating whether the spa is currently online (connected to the cloud).""" _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY + # This seems to be very noisy and not generally useful, so disable by default. + _attr_entity_registry_enabled_default = False def __init__(self, coordinator, spa): """Initialize the entity.""" super().__init__(coordinator, spa, "Online", "online") - @property - def entity_registry_enabled_default(self) -> bool: - """Return if the entity should be enabled when first added to the entity registry. - - This seems to be very noisy and not generally useful, so disable by default. - """ - return False - @property def is_on(self) -> bool: """Return true if the binary sensor is on.""" @@ -108,11 +102,7 @@ class SmartTubReminder(SmartTubEntity, BinarySensorEntity): f"{reminder.name.title()} Reminder", ) self.reminder_id = reminder.id - - @property - def unique_id(self): - """Return a unique id for this sensor.""" - return f"{self.spa.id}-reminder-{self.reminder_id}" + self._attr_unique_id = f"{spa.id}-reminder-{reminder.id}" @property def reminder(self) -> SpaReminder: diff --git a/homeassistant/components/smarttub/climate.py b/homeassistant/components/smarttub/climate.py index a938bde6fd1..b2d4fbf17c4 100644 --- a/homeassistant/components/smarttub/climate.py +++ b/homeassistant/components/smarttub/climate.py @@ -64,6 +64,7 @@ class SmartTubThermostat(SmartTubEntity, ClimateEntity): ClimateEntityFeature.PRESET_MODE | ClimateEntityFeature.TARGET_TEMPERATURE ) _attr_temperature_unit = UnitOfTemperature.CELSIUS + _attr_preset_modes = list(PRESET_MODES.values()) def __init__(self, coordinator, spa): """Initialize the entity.""" @@ -104,11 +105,6 @@ class SmartTubThermostat(SmartTubEntity, ClimateEntity): """Return the current preset mode.""" return PRESET_MODES[self.spa_status.heat_mode] - @property - def preset_modes(self): - """Return the available preset modes.""" - return list(PRESET_MODES.values()) - @property def current_temperature(self): """Return the current water temperature.""" diff --git a/homeassistant/components/smarttub/entity.py b/homeassistant/components/smarttub/entity.py index 7f2a739c26e..6e6cb00a7d3 100644 --- a/homeassistant/components/smarttub/entity.py +++ b/homeassistant/components/smarttub/entity.py @@ -25,27 +25,14 @@ class SmartTubEntity(CoordinatorEntity): super().__init__(coordinator) self.spa = spa - self._entity_name = entity_name - - @property - def unique_id(self) -> str: - """Return a unique id for the entity.""" - return f"{self.spa.id}-{self._entity_name}" - - @property - def device_info(self) -> DeviceInfo: - """Return device info.""" - return DeviceInfo( - identifiers={(DOMAIN, self.spa.id)}, - manufacturer=self.spa.brand, - model=self.spa.model, + self._attr_unique_id = f"{spa.id}-{entity_name}" + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, spa.id)}, + manufacturer=spa.brand, + model=spa.model, ) - - @property - def name(self) -> str: - """Return the name of the entity.""" spa_name = get_spa_name(self.spa) - return f"{spa_name} {self._entity_name}" + self._attr_name = f"{spa_name} {entity_name}" @property def spa_status(self) -> smarttub.SpaState: @@ -57,12 +44,12 @@ class SmartTubEntity(CoordinatorEntity): class SmartTubSensorBase(SmartTubEntity): """Base class for SmartTub sensors.""" - def __init__(self, coordinator, spa, sensor_name, attr_name): + def __init__(self, coordinator, spa, sensor_name, state_key): """Initialize the entity.""" super().__init__(coordinator, spa, sensor_name) - self._attr_name = attr_name + self._state_key = state_key @property def _state(self): """Retrieve the underlying state from the spa.""" - return getattr(self.spa_status, self._attr_name) + return getattr(self.spa_status, self._state_key) diff --git a/homeassistant/components/smarttub/light.py b/homeassistant/components/smarttub/light.py index f7e229449e0..d89cdba3367 100644 --- a/homeassistant/components/smarttub/light.py +++ b/homeassistant/components/smarttub/light.py @@ -53,23 +53,15 @@ class SmartTubLight(SmartTubEntity, LightEntity): """Initialize the entity.""" super().__init__(coordinator, light.spa, "light") self.light_zone = light.zone + self._attr_unique_id = f"{super().unique_id}-{light.zone}" + spa_name = get_spa_name(self.spa) + self._attr_name = f"{spa_name} Light {light.zone}" @property def light(self) -> SpaLight: """Return the underlying SpaLight object for this entity.""" return self.coordinator.data[self.spa.id][ATTR_LIGHTS][self.light_zone] - @property - def unique_id(self) -> str: - """Return a unique ID for this light entity.""" - return f"{super().unique_id}-{self.light_zone}" - - @property - def name(self) -> str: - """Return a name for this light entity.""" - spa_name = get_spa_name(self.spa) - return f"{spa_name} Light {self.light_zone}" - @property def brightness(self): """Return the brightness of this light between 0..255.""" diff --git a/homeassistant/components/smarttub/switch.py b/homeassistant/components/smarttub/switch.py index e105963bc01..aeeca46aaa9 100644 --- a/homeassistant/components/smarttub/switch.py +++ b/homeassistant/components/smarttub/switch.py @@ -38,17 +38,13 @@ class SmartTubPump(SmartTubEntity, SwitchEntity): super().__init__(coordinator, pump.spa, "pump") self.pump_id = pump.id self.pump_type = pump.type + self._attr_unique_id = f"{super().unique_id}-{pump.id}" @property def pump(self) -> SpaPump: """Return the underlying SpaPump object for this entity.""" return self.coordinator.data[self.spa.id][ATTR_PUMPS][self.pump_id] - @property - def unique_id(self) -> str: - """Return a unique ID for this pump entity.""" - return f"{super().unique_id}-{self.pump_id}" - @property def name(self) -> str: """Return a name for this pump entity."""