2018-12-17 19:40:57 +00:00
|
|
|
"""Support for ESPHome binary sensors."""
|
2021-03-17 22:49:01 +00:00
|
|
|
from __future__ import annotations
|
2019-05-29 11:33:49 +00:00
|
|
|
|
2022-11-29 13:07:56 +00:00
|
|
|
from contextlib import suppress
|
|
|
|
|
2019-05-29 11:33:49 +00:00
|
|
|
from aioesphomeapi import BinarySensorInfo, BinarySensorState
|
2018-12-17 19:40:57 +00:00
|
|
|
|
2022-11-29 13:07:56 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorDeviceClass,
|
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2021-07-12 20:56:10 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import EsphomeEntity, platform_async_setup_entry
|
2018-12-17 19:40:57 +00:00
|
|
|
|
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2018-12-17 19:40:57 +00:00
|
|
|
"""Set up ESPHome binary sensors based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
component_key="binary_sensor",
|
|
|
|
info_type=BinarySensorInfo,
|
|
|
|
entity_type=EsphomeBinarySensor,
|
|
|
|
state_type=BinarySensorState,
|
2018-12-17 19:40:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
class EsphomeBinarySensor(
|
|
|
|
EsphomeEntity[BinarySensorInfo, BinarySensorState], BinarySensorEntity
|
|
|
|
):
|
|
|
|
"""A binary sensor implementation for ESPHome."""
|
2018-12-17 19:40:57 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-17 22:49:01 +00:00
|
|
|
def is_on(self) -> bool | None:
|
2018-12-17 19:40:57 +00:00
|
|
|
"""Return true if the binary sensor is on."""
|
|
|
|
if self._static_info.is_status_binary_sensor:
|
|
|
|
# Status binary sensors indicated connected state.
|
|
|
|
# So in their case what's usually _availability_ is now state
|
|
|
|
return self._entry_data.available
|
2021-07-12 20:56:10 +00:00
|
|
|
if not self._has_state:
|
2018-12-17 19:40:57 +00:00
|
|
|
return None
|
2019-11-12 17:26:46 +00:00
|
|
|
if self._state.missing_state:
|
|
|
|
return None
|
2018-12-17 19:40:57 +00:00
|
|
|
return self._state.state
|
|
|
|
|
|
|
|
@property
|
2022-11-29 13:07:56 +00:00
|
|
|
def device_class(self) -> BinarySensorDeviceClass | None:
|
2018-12-17 19:40:57 +00:00
|
|
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
2022-11-29 13:07:56 +00:00
|
|
|
with suppress(ValueError):
|
|
|
|
return BinarySensorDeviceClass(self._static_info.device_class)
|
|
|
|
return None
|
2018-12-17 19:40:57 +00:00
|
|
|
|
|
|
|
@property
|
2019-04-16 20:48:46 +00:00
|
|
|
def available(self) -> bool:
|
2018-12-17 19:40:57 +00:00
|
|
|
"""Return True if entity is available."""
|
|
|
|
if self._static_info.is_status_binary_sensor:
|
|
|
|
return True
|
2018-12-18 18:04:50 +00:00
|
|
|
return super().available
|