From 89493f2d7f64110288db4253a2909f5d73c30c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Roy?= Date: Tue, 26 Jul 2022 20:36:41 -0700 Subject: [PATCH] Add occupancy sensor to the BAF integration (#75793) Co-authored-by: J. Nick Koston --- homeassistant/components/baf/__init__.py | 1 + homeassistant/components/baf/binary_sensor.py | 80 +++++++++++++++++++ homeassistant/components/baf/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 homeassistant/components/baf/binary_sensor.py diff --git a/homeassistant/components/baf/__init__.py b/homeassistant/components/baf/__init__.py index 9bb056ece4f..7e80341deab 100644 --- a/homeassistant/components/baf/__init__.py +++ b/homeassistant/components/baf/__init__.py @@ -15,6 +15,7 @@ from .const import DOMAIN, QUERY_INTERVAL, RUN_TIMEOUT from .models import BAFData PLATFORMS: list[Platform] = [ + Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.FAN, Platform.LIGHT, diff --git a/homeassistant/components/baf/binary_sensor.py b/homeassistant/components/baf/binary_sensor.py new file mode 100644 index 00000000000..8a00a7e8bd5 --- /dev/null +++ b/homeassistant/components/baf/binary_sensor.py @@ -0,0 +1,80 @@ +"""Support for Big Ass Fans binary sensors.""" +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from typing import Optional, cast + +from aiobafi6 import Device + +from homeassistant import config_entries +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .entity import BAFEntity +from .models import BAFData + + +@dataclass +class BAFBinarySensorDescriptionMixin: + """Required values for BAF binary sensors.""" + + value_fn: Callable[[Device], bool | None] + + +@dataclass +class BAFBinarySensorDescription( + BinarySensorEntityDescription, + BAFBinarySensorDescriptionMixin, +): + """Class describing BAF binary sensor entities.""" + + +OCCUPANCY_SENSORS = ( + BAFBinarySensorDescription( + key="occupancy", + name="Occupancy", + device_class=BinarySensorDeviceClass.OCCUPANCY, + value_fn=lambda device: cast(Optional[bool], device.fan_occupancy_detected), + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: config_entries.ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up BAF binary sensors.""" + data: BAFData = hass.data[DOMAIN][entry.entry_id] + device = data.device + sensors_descriptions: list[BAFBinarySensorDescription] = [] + if device.has_occupancy: + sensors_descriptions.extend(OCCUPANCY_SENSORS) + async_add_entities( + BAFBinarySensor(device, description) for description in sensors_descriptions + ) + + +class BAFBinarySensor(BAFEntity, 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, f"{device.name} {description.name}") + 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) diff --git a/homeassistant/components/baf/manifest.json b/homeassistant/components/baf/manifest.json index 821ad1a21cb..15e0272b2b0 100644 --- a/homeassistant/components/baf/manifest.json +++ b/homeassistant/components/baf/manifest.json @@ -3,7 +3,7 @@ "name": "Big Ass Fans", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/baf", - "requirements": ["aiobafi6==0.6.0"], + "requirements": ["aiobafi6==0.7.0"], "codeowners": ["@bdraco", "@jfroy"], "iot_class": "local_push", "zeroconf": [ diff --git a/requirements_all.txt b/requirements_all.txt index 7a8f314df9f..f1e14dc0aeb 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -128,7 +128,7 @@ aioasuswrt==1.4.0 aioazuredevops==1.3.5 # homeassistant.components.baf -aiobafi6==0.6.0 +aiobafi6==0.7.0 # homeassistant.components.aws aiobotocore==2.1.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index bc952f553db..d52446c4a26 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -115,7 +115,7 @@ aioasuswrt==1.4.0 aioazuredevops==1.3.5 # homeassistant.components.baf -aiobafi6==0.6.0 +aiobafi6==0.7.0 # homeassistant.components.aws aiobotocore==2.1.0