mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Add fjäråskupan binary_sensor (#54920)
* Add fjäråskupan binary_sensor * Switch to entity description * Type check constructor
This commit is contained in:
parent
3048923dc2
commit
dd8542e01f
@ -318,6 +318,7 @@ omit =
|
|||||||
homeassistant/components/fitbit/*
|
homeassistant/components/fitbit/*
|
||||||
homeassistant/components/fixer/sensor.py
|
homeassistant/components/fixer/sensor.py
|
||||||
homeassistant/components/fjaraskupan/__init__.py
|
homeassistant/components/fjaraskupan/__init__.py
|
||||||
|
homeassistant/components/fjaraskupan/binary_sensor.py
|
||||||
homeassistant/components/fjaraskupan/const.py
|
homeassistant/components/fjaraskupan/const.py
|
||||||
homeassistant/components/fjaraskupan/fan.py
|
homeassistant/components/fjaraskupan/fan.py
|
||||||
homeassistant/components/fleetgo/device_tracker.py
|
homeassistant/components/fleetgo/device_tracker.py
|
||||||
|
@ -23,7 +23,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|||||||
|
|
||||||
from .const import DISPATCH_DETECTION, DOMAIN
|
from .const import DISPATCH_DETECTION, DOMAIN
|
||||||
|
|
||||||
PLATFORMS = ["fan"]
|
PLATFORMS = ["binary_sensor", "fan"]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
95
homeassistant/components/fjaraskupan/binary_sensor.py
Normal file
95
homeassistant/components/fjaraskupan/binary_sensor.py
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
"""Support for sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
from fjaraskupan import Device, State
|
||||||
|
|
||||||
|
from homeassistant.components.binary_sensor import (
|
||||||
|
DEVICE_CLASS_PROBLEM,
|
||||||
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
|
)
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.update_coordinator import (
|
||||||
|
CoordinatorEntity,
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
)
|
||||||
|
|
||||||
|
from . import DeviceState, async_setup_entry_platform
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class EntityDescription(BinarySensorEntityDescription):
|
||||||
|
"""Entity description."""
|
||||||
|
|
||||||
|
is_on: Callable = lambda _: False
|
||||||
|
|
||||||
|
|
||||||
|
SENSORS = (
|
||||||
|
EntityDescription(
|
||||||
|
key="grease-filter",
|
||||||
|
name="Grease Filter",
|
||||||
|
device_class=DEVICE_CLASS_PROBLEM,
|
||||||
|
is_on=lambda state: state.grease_filter_full,
|
||||||
|
),
|
||||||
|
EntityDescription(
|
||||||
|
key="carbon-filter",
|
||||||
|
name="Carbon Filter",
|
||||||
|
device_class=DEVICE_CLASS_PROBLEM,
|
||||||
|
is_on=lambda state: state.carbon_filter_full,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up sensors dynamically through discovery."""
|
||||||
|
|
||||||
|
def _constructor(device_state: DeviceState) -> list[Entity]:
|
||||||
|
return [
|
||||||
|
BinarySensor(
|
||||||
|
device_state.coordinator,
|
||||||
|
device_state.device,
|
||||||
|
device_state.device_info,
|
||||||
|
entity_description,
|
||||||
|
)
|
||||||
|
for entity_description in SENSORS
|
||||||
|
]
|
||||||
|
|
||||||
|
async_setup_entry_platform(hass, config_entry, async_add_entities, _constructor)
|
||||||
|
|
||||||
|
|
||||||
|
class BinarySensor(CoordinatorEntity[State], BinarySensorEntity):
|
||||||
|
"""Grease filter sensor."""
|
||||||
|
|
||||||
|
entity_description: EntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: DataUpdateCoordinator[State],
|
||||||
|
device: Device,
|
||||||
|
device_info: DeviceInfo,
|
||||||
|
entity_description: EntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Init sensor."""
|
||||||
|
super().__init__(coordinator)
|
||||||
|
self.entity_description = entity_description
|
||||||
|
|
||||||
|
self._attr_unique_id = f"{device.address}-{entity_description.key}"
|
||||||
|
self._attr_device_info = device_info
|
||||||
|
self._attr_name = f"{device_info['name']} {entity_description.name}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool | None:
|
||||||
|
"""Return true if the binary sensor is on."""
|
||||||
|
if data := self.coordinator.data:
|
||||||
|
return self.entity_description.is_on(data)
|
||||||
|
return None
|
Loading…
x
Reference in New Issue
Block a user