1
mirror of https://github.com/home-assistant/core synced 2024-10-04 07:58:43 +02:00

Add permobil binary sensor (#112130)

* add binary sensor

* remove _LOGGER and mixin
This commit is contained in:
Isak Nyberg 2024-03-04 20:11:14 +01:00 committed by GitHub
parent 119df48aac
commit 0663a4be3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 79 additions and 2 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -69,6 +69,11 @@
"record_distance": {
"name": "Record distance"
}
},
"binary_sensor": {
"is_charging": {
"name": "Is charging"
}
}
}
}