diff --git a/.coveragerc b/.coveragerc index d8d8bbdf80d..eeffb341fd8 100644 --- a/.coveragerc +++ b/.coveragerc @@ -121,6 +121,7 @@ omit = homeassistant/components/awair/coordinator.py homeassistant/components/azure_service_bus/* homeassistant/components/baf/__init__.py + homeassistant/components/baf/binary_sensor.py homeassistant/components/baf/climate.py homeassistant/components/baf/entity.py homeassistant/components/baf/fan.py diff --git a/homeassistant/components/baf/binary_sensor.py b/homeassistant/components/baf/binary_sensor.py index b1076a99f8a..7c855711712 100644 --- a/homeassistant/components/baf/binary_sensor.py +++ b/homeassistant/components/baf/binary_sensor.py @@ -17,7 +17,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import BAFConfigEntry -from .entity import BAFEntity +from .entity import BAFDescriptionEntity @dataclass(frozen=True, kw_only=True) @@ -45,27 +45,18 @@ async def async_setup_entry( ) -> None: """Set up BAF binary sensors.""" device = entry.runtime_data - sensors_descriptions: list[BAFBinarySensorDescription] = [] if device.has_occupancy: - sensors_descriptions.extend(OCCUPANCY_SENSORS) - async_add_entities( - BAFBinarySensor(device, description) for description in sensors_descriptions - ) + async_add_entities( + BAFBinarySensor(device, description) for description in OCCUPANCY_SENSORS + ) -class BAFBinarySensor(BAFEntity, BinarySensorEntity): +class BAFBinarySensor(BAFDescriptionEntity, BinarySensorEntity): """BAF binary sensor.""" entity_description: BAFBinarySensorDescription - def __init__(self, device: Device, description: BAFBinarySensorDescription) -> None: - """Initialize the entity.""" - self.entity_description = description - super().__init__(device) - self._attr_unique_id = f"{self._device.mac_address}-{description.key}" - @callback def _async_update_attrs(self) -> None: """Update attrs from device.""" - description = self.entity_description - self._attr_is_on = description.value_fn(self._device) + self._attr_is_on = self.entity_description.value_fn(self._device) diff --git a/homeassistant/components/baf/entity.py b/homeassistant/components/baf/entity.py index 487e601b542..6bb9dbfeca7 100644 --- a/homeassistant/components/baf/entity.py +++ b/homeassistant/components/baf/entity.py @@ -7,7 +7,7 @@ from aiobafi6 import Device from homeassistant.core import callback from homeassistant.helpers import device_registry as dr from homeassistant.helpers.device_registry import DeviceInfo, format_mac -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.entity import Entity, EntityDescription class BAFEntity(Entity): @@ -47,3 +47,13 @@ class BAFEntity(Entity): async def async_will_remove_from_hass(self) -> None: """Remove data updated listener after this object has been initialized.""" self._device.remove_callback(self._async_update_from_device) + + +class BAFDescriptionEntity(BAFEntity): + """Base class for baf entities that use an entity description.""" + + def __init__(self, device: Device, description: EntityDescription) -> None: + """Initialize the entity.""" + self.entity_description = description + super().__init__(device) + self._attr_unique_id = f"{device.mac_address}-{description.key}" diff --git a/homeassistant/components/baf/number.py b/homeassistant/components/baf/number.py index bf9e837eea1..a2e5e704e4d 100644 --- a/homeassistant/components/baf/number.py +++ b/homeassistant/components/baf/number.py @@ -19,7 +19,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import BAFConfigEntry from .const import HALF_DAY_SECS, ONE_DAY_SECS, ONE_MIN_SECS, SPEED_RANGE -from .entity import BAFEntity +from .entity import BAFDescriptionEntity @dataclass(frozen=True, kw_only=True) @@ -130,17 +130,11 @@ async def async_setup_entry( async_add_entities(BAFNumber(device, description) for description in descriptions) -class BAFNumber(BAFEntity, NumberEntity): +class BAFNumber(BAFDescriptionEntity, NumberEntity): """BAF number.""" entity_description: BAFNumberDescription - def __init__(self, device: Device, description: BAFNumberDescription) -> None: - """Initialize the entity.""" - self.entity_description = description - super().__init__(device) - self._attr_unique_id = f"{self._device.mac_address}-{description.key}" - @callback def _async_update_attrs(self) -> None: """Update attrs from device.""" diff --git a/homeassistant/components/baf/sensor.py b/homeassistant/components/baf/sensor.py index a97e2945564..7e664254a38 100644 --- a/homeassistant/components/baf/sensor.py +++ b/homeassistant/components/baf/sensor.py @@ -24,7 +24,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import BAFConfigEntry -from .entity import BAFEntity +from .entity import BAFDescriptionEntity @dataclass(frozen=True, kw_only=True) @@ -111,19 +111,12 @@ async def async_setup_entry( ) -class BAFSensor(BAFEntity, SensorEntity): +class BAFSensor(BAFDescriptionEntity, SensorEntity): """BAF sensor.""" entity_description: BAFSensorDescription - def __init__(self, device: Device, description: BAFSensorDescription) -> None: - """Initialize the entity.""" - self.entity_description = description - super().__init__(device) - self._attr_unique_id = f"{self._device.mac_address}-{description.key}" - @callback def _async_update_attrs(self) -> None: """Update attrs from device.""" - description = self.entity_description - self._attr_native_value = description.value_fn(self._device) + self._attr_native_value = self.entity_description.value_fn(self._device) diff --git a/homeassistant/components/baf/switch.py b/homeassistant/components/baf/switch.py index 789ea365d6d..e18e26ddcaa 100644 --- a/homeassistant/components/baf/switch.py +++ b/homeassistant/components/baf/switch.py @@ -14,7 +14,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import BAFConfigEntry -from .entity import BAFEntity +from .entity import BAFDescriptionEntity @dataclass(frozen=True, kw_only=True) @@ -118,17 +118,11 @@ async def async_setup_entry( async_add_entities(BAFSwitch(device, description) for description in descriptions) -class BAFSwitch(BAFEntity, SwitchEntity): +class BAFSwitch(BAFDescriptionEntity, SwitchEntity): """BAF switch component.""" entity_description: BAFSwitchDescription - def __init__(self, device: Device, description: BAFSwitchDescription) -> None: - """Initialize the entity.""" - self.entity_description = description - super().__init__(device) - self._attr_unique_id = f"{self._device.mac_address}-{description.key}" - @callback def _async_update_attrs(self) -> None: """Update attrs from device."""