From 0663a4be3be01027246eb0c72a5b1a108b101d89 Mon Sep 17 00:00:00 2001 From: Isak Nyberg <36712644+IsakNyberg@users.noreply.github.com> Date: Mon, 4 Mar 2024 20:11:14 +0100 Subject: [PATCH] Add permobil binary sensor (#112130) * add binary sensor * remove _LOGGER and mixin --- .coveragerc | 1 + homeassistant/components/permobil/__init__.py | 2 +- .../components/permobil/binary_sensor.py | 71 +++++++++++++++++++ .../components/permobil/coordinator.py | 2 +- .../components/permobil/strings.json | 5 ++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 homeassistant/components/permobil/binary_sensor.py diff --git a/.coveragerc b/.coveragerc index b020819d3be..d95c636e22c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -983,6 +983,7 @@ omit = homeassistant/components/pandora/media_player.py homeassistant/components/pencom/switch.py homeassistant/components/permobil/__init__.py + homeassistant/components/permobil/binary_sensor.py homeassistant/components/permobil/coordinator.py homeassistant/components/permobil/entity.py homeassistant/components/permobil/sensor.py diff --git a/homeassistant/components/permobil/__init__.py b/homeassistant/components/permobil/__init__.py index 0213fb6a4b6..18025220852 100644 --- a/homeassistant/components/permobil/__init__.py +++ b/homeassistant/components/permobil/__init__.py @@ -21,7 +21,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import APPLICATION, DOMAIN from .coordinator import MyPermobilCoordinator -PLATFORMS: list[Platform] = [Platform.SENSOR] +PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/permobil/binary_sensor.py b/homeassistant/components/permobil/binary_sensor.py new file mode 100644 index 00000000000..afa17bde04f --- /dev/null +++ b/homeassistant/components/permobil/binary_sensor.py @@ -0,0 +1,71 @@ +"""Platform for binary sensor integration.""" +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from typing import Any + +from mypermobil import BATTERY_CHARGING + +from homeassistant import config_entries +from homeassistant.components.binary_sensor import ( + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .coordinator import MyPermobilCoordinator +from .entity import PermobilEntity + + +@dataclass(frozen=True, kw_only=True) +class PermobilBinarySensorEntityDescription(BinarySensorEntityDescription): + """Describes Permobil binary sensor entity.""" + + is_on_fn: Callable[[Any], bool] + available_fn: Callable[[Any], bool] + + +BINARY_SENSOR_DESCRIPTIONS: tuple[PermobilBinarySensorEntityDescription, ...] = ( + PermobilBinarySensorEntityDescription( + is_on_fn=lambda data: data.battery[BATTERY_CHARGING[0]], + available_fn=lambda data: BATTERY_CHARGING[0] in data.battery, + key="is_charging", + translation_key="is_charging", + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: config_entries.ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Create and setup the binary sensor.""" + + coordinator: MyPermobilCoordinator = hass.data[DOMAIN][config_entry.entry_id] + + async_add_entities( + PermobilbinarySensor(coordinator=coordinator, description=description) + for description in BINARY_SENSOR_DESCRIPTIONS + ) + + +class PermobilbinarySensor(PermobilEntity, BinarySensorEntity): + """Representation of a Binary Sensor.""" + + entity_description: PermobilBinarySensorEntityDescription + + @property + def is_on(self) -> bool: + """Return True if the wheelchair is charging.""" + return self.entity_description.is_on_fn(self.coordinator.data) + + @property + def available(self) -> bool: + """Return True if the sensor has value.""" + return super().available and self.entity_description.available_fn( + self.coordinator.data + ) diff --git a/homeassistant/components/permobil/coordinator.py b/homeassistant/components/permobil/coordinator.py index 3695236cdf0..f505e73fa23 100644 --- a/homeassistant/components/permobil/coordinator.py +++ b/homeassistant/components/permobil/coordinator.py @@ -17,7 +17,7 @@ _LOGGER = logging.getLogger(__name__) class MyPermobilData: """MyPermobil data stored in the DataUpdateCoordinator.""" - battery: dict[str, str | float | int | list | dict] + battery: dict[str, str | float | int | bool | list | dict] daily_usage: dict[str, str | float | int | list | dict] records: dict[str, str | float | int | list | dict] diff --git a/homeassistant/components/permobil/strings.json b/homeassistant/components/permobil/strings.json index 5070c13d9e5..d3a9290854e 100644 --- a/homeassistant/components/permobil/strings.json +++ b/homeassistant/components/permobil/strings.json @@ -69,6 +69,11 @@ "record_distance": { "name": "Record distance" } + }, + "binary_sensor": { + "is_charging": { + "name": "Is charging" + } } } }