Use entity class attributes for Blebox (#52890)

* Use entity class attributes for blebox

* rework

* Apply suggestions from code review

Co-authored-by: Franck Nijhof <git@frenck.dev>

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Robert Hillis 2021-07-12 16:52:38 -04:00 committed by GitHub
parent ab5fd70988
commit 9b2107b71f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 72 deletions

View File

@ -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,
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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