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
|
|
|
|
2023-06-21 09:00:21 +00:00
|
|
|
from aioesphomeapi import BinarySensorInfo, BinarySensorState, EntityInfo
|
2018-12-17 19:40:57 +00:00
|
|
|
|
2022-11-29 13:07:56 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorDeviceClass,
|
|
|
|
BinarySensorEntity,
|
2023-04-17 23:52:37 +00:00
|
|
|
BinarySensorEntityDescription,
|
2022-11-29 13:07:56 +00:00
|
|
|
)
|
2021-07-12 20:56:10 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-06-21 09:00:21 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-07-12 20:56:10 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-02-02 11:34:01 +00:00
|
|
|
from homeassistant.util.enum import try_parse_enum
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2023-04-17 23:52:37 +00:00
|
|
|
from .domain_data import DomainData
|
2023-06-25 03:12:36 +00:00
|
|
|
from .entity import (
|
|
|
|
EsphomeAssistEntity,
|
|
|
|
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,
|
|
|
|
info_type=BinarySensorInfo,
|
|
|
|
entity_type=EsphomeBinarySensor,
|
|
|
|
state_type=BinarySensorState,
|
2018-12-17 19:40:57 +00:00
|
|
|
)
|
|
|
|
|
2023-04-17 23:52:37 +00:00
|
|
|
entry_data = DomainData.get(hass).get_entry_data(entry)
|
|
|
|
assert entry_data.device_info is not None
|
|
|
|
if entry_data.device_info.voice_assistant_version:
|
2023-04-27 02:13:21 +00:00
|
|
|
async_add_entities([EsphomeAssistInProgressBinarySensor(entry_data)])
|
2023-04-17 23:52:37 +00:00
|
|
|
|
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
|
2023-06-25 03:09:26 +00:00
|
|
|
if not self._has_state or self._state.missing_state:
|
2023-06-22 17:55:28 +00:00
|
|
|
return None
|
2023-06-25 03:09:26 +00:00
|
|
|
return self._state.state
|
2018-12-17 19:40:57 +00:00
|
|
|
|
2023-06-21 09:00:21 +00:00
|
|
|
@callback
|
|
|
|
def _on_static_info_update(self, static_info: EntityInfo) -> None:
|
|
|
|
"""Set attrs from static info."""
|
|
|
|
super()._on_static_info_update(static_info)
|
|
|
|
self._attr_device_class = try_parse_enum(
|
|
|
|
BinarySensorDeviceClass, self._static_info.device_class
|
|
|
|
)
|
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."""
|
2023-06-22 09:14:33 +00:00
|
|
|
return self._static_info.is_status_binary_sensor or super().available
|
2023-04-17 23:52:37 +00:00
|
|
|
|
|
|
|
|
2023-04-27 02:13:21 +00:00
|
|
|
class EsphomeAssistInProgressBinarySensor(EsphomeAssistEntity, BinarySensorEntity):
|
2023-04-17 23:52:37 +00:00
|
|
|
"""A binary sensor implementation for ESPHome for use with assist_pipeline."""
|
|
|
|
|
|
|
|
entity_description = BinarySensorEntityDescription(
|
2023-04-27 02:13:21 +00:00
|
|
|
key="assist_in_progress",
|
|
|
|
translation_key="assist_in_progress",
|
2023-04-17 23:52:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
"""Return true if the binary sensor is on."""
|
|
|
|
return self._entry_data.assist_pipeline_state
|