Use shorthand attributes in Smarttub (#99839)

This commit is contained in:
Joost Lekkerkerker 2023-09-12 15:17:57 +02:00 committed by GitHub
parent b5275016d4
commit 76c3a638c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 56 deletions

View File

@ -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:

View File

@ -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."""

View File

@ -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)

View File

@ -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."""

View File

@ -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."""