mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
Reduce duplicate code in baf for entities with descriptions (#119945)
* Reduce duplicate code in baf for entities with descriptions * Reduce duplicate code in baf for entities with descriptions * no cover * no cover
This commit is contained in:
parent
ef51fc0d97
commit
c686eda305
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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}"
|
||||
|
@ -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."""
|
||||
|
@ -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)
|
||||
|
@ -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."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user