From 9b2107b71f8dc5eac6718c145c64acab342e5a06 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Mon, 12 Jul 2021 16:52:38 -0400 Subject: [PATCH] Use entity class attributes for Blebox (#52890) * Use entity class attributes for blebox * rework * Apply suggestions from code review Co-authored-by: Franck Nijhof Co-authored-by: Franck Nijhof --- homeassistant/components/blebox/__init__.py | 32 ++++++------------- .../components/blebox/air_quality.py | 5 +-- homeassistant/components/blebox/climate.py | 17 ++-------- homeassistant/components/blebox/cover.py | 21 +++++------- homeassistant/components/blebox/light.py | 10 +++--- homeassistant/components/blebox/sensor.py | 16 ++++------ homeassistant/components/blebox/switch.py | 8 ++--- 7 files changed, 37 insertions(+), 72 deletions(-) diff --git a/homeassistant/components/blebox/__init__.py b/homeassistant/components/blebox/__init__.py index 33d09f460db..95b36612add 100644 --- a/homeassistant/components/blebox/__init__.py +++ b/homeassistant/components/blebox/__init__.py @@ -79,16 +79,16 @@ class BleBoxEntity(Entity): def __init__(self, feature): """Initialize a BleBox entity.""" self._feature = feature - - @property - def name(self): - """Return the internal entity name.""" - return self._feature.full_name - - @property - def unique_id(self): - """Return a unique id.""" - return self._feature.unique_id + self._attr_name = feature.full_name + self._attr_unique_id = feature.unique_id + product = feature.product + self._attr_device_info = { + "identifiers": {(DOMAIN, product.unique_id)}, + "name": product.name, + "manufacturer": product.brand, + "model": product.model, + "sw_version": product.firmware_version, + } async def async_update(self): """Update the entity state.""" @@ -96,15 +96,3 @@ class BleBoxEntity(Entity): await self._feature.async_update() except Error as ex: _LOGGER.error("Updating '%s' failed: %s", self.name, ex) - - @property - def device_info(self): - """Return device information for this entity.""" - product = self._feature.product - return { - "identifiers": {(DOMAIN, product.unique_id)}, - "name": product.name, - "manufacturer": product.brand, - "model": product.model, - "sw_version": product.firmware_version, - } diff --git a/homeassistant/components/blebox/air_quality.py b/homeassistant/components/blebox/air_quality.py index e7e9bac1f97..debf0201a3f 100644 --- a/homeassistant/components/blebox/air_quality.py +++ b/homeassistant/components/blebox/air_quality.py @@ -15,10 +15,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class BleBoxAirQualityEntity(BleBoxEntity, AirQualityEntity): """Representation of a BleBox air quality feature.""" - @property - def icon(self): - """Return the icon.""" - return "mdi:blur" + _attr_icon = "mdi:blur" @property def particulate_matter_0_1(self): diff --git a/homeassistant/components/blebox/climate.py b/homeassistant/components/blebox/climate.py index 4ee8cf9be76..59e64b772ef 100644 --- a/homeassistant/components/blebox/climate.py +++ b/homeassistant/components/blebox/climate.py @@ -25,10 +25,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class BleBoxClimateEntity(BleBoxEntity, ClimateEntity): """Representation of a BleBox climate feature (saunaBox).""" - @property - def supported_features(self): - """Return the supported climate features.""" - return SUPPORT_TARGET_TEMPERATURE + _attr_supported_features = SUPPORT_TARGET_TEMPERATURE + _attr_hvac_modes = [HVAC_MODE_OFF, HVAC_MODE_HEAT] + _attr_temperature_unit = TEMP_CELSIUS @property def hvac_mode(self): @@ -48,16 +47,6 @@ class BleBoxClimateEntity(BleBoxEntity, ClimateEntity): # NOTE: In practice, there's no need to handle case when is_heating is None return CURRENT_HVAC_HEAT if self._feature.is_heating else CURRENT_HVAC_IDLE - @property - def hvac_modes(self): - """Return a list of possible HVAC modes.""" - return [HVAC_MODE_OFF, HVAC_MODE_HEAT] - - @property - def temperature_unit(self): - """Return the temperature unit.""" - return TEMP_CELSIUS - @property def max_temp(self): """Return the maximum temperature supported.""" diff --git a/homeassistant/components/blebox/cover.py b/homeassistant/components/blebox/cover.py index 620adacf3f6..5dc6a486ed3 100644 --- a/homeassistant/components/blebox/cover.py +++ b/homeassistant/components/blebox/cover.py @@ -27,24 +27,19 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class BleBoxCoverEntity(BleBoxEntity, CoverEntity): """Representation of a BleBox cover feature.""" + def __init__(self, feature): + """Initialize a BleBox cover feature.""" + super().__init__(feature) + self._attr_device_class = BLEBOX_TO_HASS_DEVICE_CLASSES[feature.device_class] + position = SUPPORT_SET_POSITION if feature.is_slider else 0 + stop = SUPPORT_STOP if feature.has_stop else 0 + self._attr_supported_features = position | stop | SUPPORT_OPEN | SUPPORT_CLOSE + @property def state(self): """Return the equivalent HA cover state.""" return BLEBOX_TO_HASS_COVER_STATES[self._feature.state] - @property - def device_class(self): - """Return the device class.""" - return BLEBOX_TO_HASS_DEVICE_CLASSES[self._feature.device_class] - - @property - def supported_features(self): - """Return the supported cover features.""" - position = SUPPORT_SET_POSITION if self._feature.is_slider else 0 - stop = SUPPORT_STOP if self._feature.has_stop else 0 - - return position | stop | SUPPORT_OPEN | SUPPORT_CLOSE - @property def current_cover_position(self): """Return the current cover position.""" diff --git a/homeassistant/components/blebox/light.py b/homeassistant/components/blebox/light.py index 9bb7371a97a..b03cc16112c 100644 --- a/homeassistant/components/blebox/light.py +++ b/homeassistant/components/blebox/light.py @@ -29,13 +29,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class BleBoxLightEntity(BleBoxEntity, LightEntity): """Representation of BleBox lights.""" - @property - def supported_color_modes(self): - """Return supported color modes.""" - return {self.color_mode} + def __init__(self, feature): + """Initialize a BleBox light.""" + super().__init__(feature) + self._attr_supported_color_modes = {self.color_mode} @property - def is_on(self): + def is_on(self) -> bool: """Return if light is on.""" return self._feature.is_on diff --git a/homeassistant/components/blebox/sensor.py b/homeassistant/components/blebox/sensor.py index c1b9d8501c1..09bfca88776 100644 --- a/homeassistant/components/blebox/sensor.py +++ b/homeassistant/components/blebox/sensor.py @@ -17,17 +17,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class BleBoxSensorEntity(BleBoxEntity, SensorEntity): """Representation of a BleBox sensor feature.""" + def __init__(self, feature): + """Initialize a BleBox sensor feature.""" + super().__init__(feature) + self._attr_unit_of_measurement = BLEBOX_TO_UNIT_MAP[feature.unit] + self._attr_device_class = BLEBOX_TO_HASS_DEVICE_CLASSES[feature.device_class] + @property def state(self): """Return the state.""" return self._feature.current - - @property - def unit_of_measurement(self): - """Return the unit.""" - return BLEBOX_TO_UNIT_MAP[self._feature.unit] - - @property - def device_class(self): - """Return the device class.""" - return BLEBOX_TO_HASS_DEVICE_CLASSES[self._feature.device_class] diff --git a/homeassistant/components/blebox/switch.py b/homeassistant/components/blebox/switch.py index e88773db639..3769235e943 100644 --- a/homeassistant/components/blebox/switch.py +++ b/homeassistant/components/blebox/switch.py @@ -15,10 +15,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class BleBoxSwitchEntity(BleBoxEntity, SwitchEntity): """Representation of a BleBox switch feature.""" - @property - def device_class(self): - """Return the device class.""" - return BLEBOX_TO_HASS_DEVICE_CLASSES[self._feature.device_class] + def __init__(self, feature): + """Initialize a BleBox switch feature.""" + super().__init__(feature) + self._attr_device_class = BLEBOX_TO_HASS_DEVICE_CLASSES[feature.device_class] @property def is_on(self):