Add permobil binary sensor (#112130)
* add binary sensor * remove _LOGGER and mixin
This commit is contained in:
parent
119df48aac
commit
0663a4be3b
5 changed files with 79 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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__)
|
||||
|
||||
|
|
71
homeassistant/components/permobil/binary_sensor.py
Normal file
71
homeassistant/components/permobil/binary_sensor.py
Normal 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
|
||||
)
|
|
@ -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]
|
||||
|
||||
|
|
|
@ -69,6 +69,11 @@
|
|||
"record_distance": {
|
||||
"name": "Record distance"
|
||||
}
|
||||
},
|
||||
"binary_sensor": {
|
||||
"is_charging": {
|
||||
"name": "Is charging"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue