init home-assistant

This commit is contained in:
2026-11-19 15:15:32 -05:00
parent 2e81d9d300
commit feec6ef9fb
19230 changed files with 3335526 additions and 0 deletions

View File

@@ -0,0 +1,202 @@
"""Component to interface with binary sensors."""
from __future__ import annotations
from datetime import timedelta
from enum import StrEnum
import logging
from typing import Literal, final
from propcache import cached_property
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_OFF, STATE_ON, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.hass_dict import HassKey
_LOGGER = logging.getLogger(__name__)
DOMAIN = "binary_sensor"
DATA_COMPONENT: HassKey[EntityComponent[BinarySensorEntity]] = HassKey(DOMAIN)
ENTITY_ID_FORMAT = DOMAIN + ".{}"
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
SCAN_INTERVAL = timedelta(seconds=30)
class BinarySensorDeviceClass(StrEnum):
"""Device class for binary sensors."""
# On means low, Off means normal
BATTERY = "battery"
# On means charging, Off means not charging
BATTERY_CHARGING = "battery_charging"
# On means carbon monoxide detected, Off means no carbon monoxide (clear)
CO = "carbon_monoxide"
# On means cold, Off means normal
COLD = "cold"
# On means connected, Off means disconnected
CONNECTIVITY = "connectivity"
# On means open, Off means closed
DOOR = "door"
# On means open, Off means closed
GARAGE_DOOR = "garage_door"
# On means gas detected, Off means no gas (clear)
GAS = "gas"
# On means hot, Off means normal
HEAT = "heat"
# On means light detected, Off means no light
LIGHT = "light"
# On means open (unlocked), Off means closed (locked)
LOCK = "lock"
# On means wet, Off means dry
MOISTURE = "moisture"
# On means motion detected, Off means no motion (clear)
MOTION = "motion"
# On means moving, Off means not moving (stopped)
MOVING = "moving"
# On means occupied, Off means not occupied (clear)
OCCUPANCY = "occupancy"
# On means open, Off means closed
OPENING = "opening"
# On means plugged in, Off means unplugged
PLUG = "plug"
# On means power detected, Off means no power
POWER = "power"
# On means home, Off means away
PRESENCE = "presence"
# On means problem detected, Off means no problem (OK)
PROBLEM = "problem"
# On means running, Off means not running
RUNNING = "running"
# On means unsafe, Off means safe
SAFETY = "safety"
# On means smoke detected, Off means no smoke (clear)
SMOKE = "smoke"
# On means sound detected, Off means no sound (clear)
SOUND = "sound"
# On means tampering detected, Off means no tampering (clear)
TAMPER = "tamper"
# On means update available, Off means up-to-date
UPDATE = "update"
# On means vibration detected, Off means no vibration
VIBRATION = "vibration"
# On means open, Off means closed
WINDOW = "window"
DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.Coerce(BinarySensorDeviceClass))
DEVICE_CLASSES = [cls.value for cls in BinarySensorDeviceClass]
# mypy: disallow-any-generics
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Track states and offer events for binary sensors."""
component = hass.data[DATA_COMPONENT] = EntityComponent[BinarySensorEntity](
logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL
)
await component.async_setup(config)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a config entry."""
return await hass.data[DATA_COMPONENT].async_setup_entry(entry)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.data[DATA_COMPONENT].async_unload_entry(entry)
class BinarySensorEntityDescription(EntityDescription, frozen_or_thawed=True):
"""A class that describes binary sensor entities."""
device_class: BinarySensorDeviceClass | None = None
CACHED_PROPERTIES_WITH_ATTR_ = {
"device_class",
"is_on",
}
class BinarySensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Represent a binary sensor."""
entity_description: BinarySensorEntityDescription
_attr_device_class: BinarySensorDeviceClass | None
_attr_is_on: bool | None = None
_attr_state: None = None
async def async_internal_added_to_hass(self) -> None:
"""Call when the binary sensor entity is added to hass."""
await super().async_internal_added_to_hass()
if self.entity_category == EntityCategory.CONFIG:
raise HomeAssistantError(
f"Entity {self.entity_id} cannot be added as the entity category is set to config"
)
def _default_to_device_class_name(self) -> bool:
"""Return True if an unnamed entity should be named by its device class.
For binary sensors this is True if the entity has a device class.
"""
return self.device_class is not None
@cached_property
def device_class(self) -> BinarySensorDeviceClass | None:
"""Return the class of this entity."""
if hasattr(self, "_attr_device_class"):
return self._attr_device_class
if hasattr(self, "entity_description"):
return self.entity_description.device_class
return None
@cached_property
def is_on(self) -> bool | None:
"""Return true if the binary sensor is on."""
return self._attr_is_on
@final
@property
def state(self) -> Literal["on", "off"] | None:
"""Return the state of the binary sensor."""
if (is_on := self.is_on) is None:
return None
return STATE_ON if is_on else STATE_OFF

View File

@@ -0,0 +1,331 @@
"""Implement device conditions for binary sensor."""
from __future__ import annotations
import voluptuous as vol
from homeassistant.components.device_automation import CONF_IS_OFF, CONF_IS_ON
from homeassistant.const import (
CONF_CONDITION,
CONF_ENTITY_ID,
CONF_FOR,
CONF_STATE,
CONF_TYPE,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import (
condition,
config_validation as cv,
entity_registry as er,
)
from homeassistant.helpers.entity import get_device_class
from homeassistant.helpers.typing import ConfigType
from . import DOMAIN, BinarySensorDeviceClass
# mypy: disallow-any-generics
DEVICE_CLASS_NONE = "none"
CONF_IS_BAT_LOW = "is_bat_low"
CONF_IS_NOT_BAT_LOW = "is_not_bat_low"
CONF_IS_CHARGING = "is_charging"
CONF_IS_NOT_CHARGING = "is_not_charging"
CONF_IS_CO = "is_co"
CONF_IS_NO_CO = "is_no_co"
CONF_IS_COLD = "is_cold"
CONF_IS_NOT_COLD = "is_not_cold"
CONF_IS_CONNECTED = "is_connected"
CONF_IS_NOT_CONNECTED = "is_not_connected"
CONF_IS_GAS = "is_gas"
CONF_IS_NO_GAS = "is_no_gas"
CONF_IS_HOT = "is_hot"
CONF_IS_NOT_HOT = "is_not_hot"
CONF_IS_LIGHT = "is_light"
CONF_IS_NO_LIGHT = "is_no_light"
CONF_IS_LOCKED = "is_locked"
CONF_IS_NOT_LOCKED = "is_not_locked"
CONF_IS_MOIST = "is_moist"
CONF_IS_NOT_MOIST = "is_not_moist"
CONF_IS_MOTION = "is_motion"
CONF_IS_NO_MOTION = "is_no_motion"
CONF_IS_MOVING = "is_moving"
CONF_IS_NOT_MOVING = "is_not_moving"
CONF_IS_OCCUPIED = "is_occupied"
CONF_IS_NOT_OCCUPIED = "is_not_occupied"
CONF_IS_PLUGGED_IN = "is_plugged_in"
CONF_IS_NOT_PLUGGED_IN = "is_not_plugged_in"
CONF_IS_POWERED = "is_powered"
CONF_IS_NOT_POWERED = "is_not_powered"
CONF_IS_PRESENT = "is_present"
CONF_IS_NOT_PRESENT = "is_not_present"
CONF_IS_PROBLEM = "is_problem"
CONF_IS_NO_PROBLEM = "is_no_problem"
CONF_IS_RUNNING = "is_running"
CONF_IS_NOT_RUNNING = "is_not_running"
CONF_IS_UNSAFE = "is_unsafe"
CONF_IS_NOT_UNSAFE = "is_not_unsafe"
CONF_IS_SMOKE = "is_smoke"
CONF_IS_NO_SMOKE = "is_no_smoke"
CONF_IS_SOUND = "is_sound"
CONF_IS_NO_SOUND = "is_no_sound"
CONF_IS_TAMPERED = "is_tampered"
CONF_IS_NOT_TAMPERED = "is_not_tampered"
CONF_IS_UPDATE = "is_update"
CONF_IS_NO_UPDATE = "is_no_update"
CONF_IS_VIBRATION = "is_vibration"
CONF_IS_NO_VIBRATION = "is_no_vibration"
CONF_IS_OPEN = "is_open"
CONF_IS_NOT_OPEN = "is_not_open"
IS_ON = [
CONF_IS_BAT_LOW,
CONF_IS_CHARGING,
CONF_IS_CO,
CONF_IS_COLD,
CONF_IS_CONNECTED,
CONF_IS_GAS,
CONF_IS_HOT,
CONF_IS_LIGHT,
CONF_IS_NOT_LOCKED,
CONF_IS_MOIST,
CONF_IS_MOTION,
CONF_IS_MOVING,
CONF_IS_OCCUPIED,
CONF_IS_OPEN,
CONF_IS_PLUGGED_IN,
CONF_IS_POWERED,
CONF_IS_PRESENT,
CONF_IS_PROBLEM,
CONF_IS_RUNNING,
CONF_IS_SMOKE,
CONF_IS_SOUND,
CONF_IS_TAMPERED,
CONF_IS_UPDATE,
CONF_IS_UNSAFE,
CONF_IS_VIBRATION,
CONF_IS_ON,
]
IS_OFF = [
CONF_IS_NOT_BAT_LOW,
CONF_IS_NOT_CHARGING,
CONF_IS_NOT_COLD,
CONF_IS_NOT_CONNECTED,
CONF_IS_NOT_HOT,
CONF_IS_LOCKED,
CONF_IS_NOT_MOIST,
CONF_IS_NOT_MOVING,
CONF_IS_NOT_OCCUPIED,
CONF_IS_NOT_OPEN,
CONF_IS_NOT_PLUGGED_IN,
CONF_IS_NOT_POWERED,
CONF_IS_NOT_PRESENT,
CONF_IS_NOT_TAMPERED,
CONF_IS_NOT_UNSAFE,
CONF_IS_NO_CO,
CONF_IS_NO_GAS,
CONF_IS_NO_LIGHT,
CONF_IS_NO_MOTION,
CONF_IS_NO_PROBLEM,
CONF_IS_NOT_RUNNING,
CONF_IS_NO_SMOKE,
CONF_IS_NO_SOUND,
CONF_IS_NO_UPDATE,
CONF_IS_NO_VIBRATION,
CONF_IS_OFF,
]
ENTITY_CONDITIONS = {
BinarySensorDeviceClass.BATTERY: [
{CONF_TYPE: CONF_IS_BAT_LOW},
{CONF_TYPE: CONF_IS_NOT_BAT_LOW},
],
BinarySensorDeviceClass.BATTERY_CHARGING: [
{CONF_TYPE: CONF_IS_CHARGING},
{CONF_TYPE: CONF_IS_NOT_CHARGING},
],
BinarySensorDeviceClass.CO: [
{CONF_TYPE: CONF_IS_CO},
{CONF_TYPE: CONF_IS_NO_CO},
],
BinarySensorDeviceClass.COLD: [
{CONF_TYPE: CONF_IS_COLD},
{CONF_TYPE: CONF_IS_NOT_COLD},
],
BinarySensorDeviceClass.CONNECTIVITY: [
{CONF_TYPE: CONF_IS_CONNECTED},
{CONF_TYPE: CONF_IS_NOT_CONNECTED},
],
BinarySensorDeviceClass.DOOR: [
{CONF_TYPE: CONF_IS_OPEN},
{CONF_TYPE: CONF_IS_NOT_OPEN},
],
BinarySensorDeviceClass.GARAGE_DOOR: [
{CONF_TYPE: CONF_IS_OPEN},
{CONF_TYPE: CONF_IS_NOT_OPEN},
],
BinarySensorDeviceClass.GAS: [
{CONF_TYPE: CONF_IS_GAS},
{CONF_TYPE: CONF_IS_NO_GAS},
],
BinarySensorDeviceClass.HEAT: [
{CONF_TYPE: CONF_IS_HOT},
{CONF_TYPE: CONF_IS_NOT_HOT},
],
BinarySensorDeviceClass.LIGHT: [
{CONF_TYPE: CONF_IS_LIGHT},
{CONF_TYPE: CONF_IS_NO_LIGHT},
],
BinarySensorDeviceClass.LOCK: [
{CONF_TYPE: CONF_IS_LOCKED},
{CONF_TYPE: CONF_IS_NOT_LOCKED},
],
BinarySensorDeviceClass.MOISTURE: [
{CONF_TYPE: CONF_IS_MOIST},
{CONF_TYPE: CONF_IS_NOT_MOIST},
],
BinarySensorDeviceClass.MOTION: [
{CONF_TYPE: CONF_IS_MOTION},
{CONF_TYPE: CONF_IS_NO_MOTION},
],
BinarySensorDeviceClass.MOVING: [
{CONF_TYPE: CONF_IS_MOVING},
{CONF_TYPE: CONF_IS_NOT_MOVING},
],
BinarySensorDeviceClass.OCCUPANCY: [
{CONF_TYPE: CONF_IS_OCCUPIED},
{CONF_TYPE: CONF_IS_NOT_OCCUPIED},
],
BinarySensorDeviceClass.OPENING: [
{CONF_TYPE: CONF_IS_OPEN},
{CONF_TYPE: CONF_IS_NOT_OPEN},
],
BinarySensorDeviceClass.PLUG: [
{CONF_TYPE: CONF_IS_PLUGGED_IN},
{CONF_TYPE: CONF_IS_NOT_PLUGGED_IN},
],
BinarySensorDeviceClass.POWER: [
{CONF_TYPE: CONF_IS_POWERED},
{CONF_TYPE: CONF_IS_NOT_POWERED},
],
BinarySensorDeviceClass.PRESENCE: [
{CONF_TYPE: CONF_IS_PRESENT},
{CONF_TYPE: CONF_IS_NOT_PRESENT},
],
BinarySensorDeviceClass.PROBLEM: [
{CONF_TYPE: CONF_IS_PROBLEM},
{CONF_TYPE: CONF_IS_NO_PROBLEM},
],
BinarySensorDeviceClass.RUNNING: [
{CONF_TYPE: CONF_IS_RUNNING},
{CONF_TYPE: CONF_IS_NOT_RUNNING},
],
BinarySensorDeviceClass.SAFETY: [
{CONF_TYPE: CONF_IS_UNSAFE},
{CONF_TYPE: CONF_IS_NOT_UNSAFE},
],
BinarySensorDeviceClass.SMOKE: [
{CONF_TYPE: CONF_IS_SMOKE},
{CONF_TYPE: CONF_IS_NO_SMOKE},
],
BinarySensorDeviceClass.SOUND: [
{CONF_TYPE: CONF_IS_SOUND},
{CONF_TYPE: CONF_IS_NO_SOUND},
],
BinarySensorDeviceClass.TAMPER: [
{CONF_TYPE: CONF_IS_TAMPERED},
{CONF_TYPE: CONF_IS_NOT_TAMPERED},
],
BinarySensorDeviceClass.UPDATE: [
{CONF_TYPE: CONF_IS_UPDATE},
{CONF_TYPE: CONF_IS_NO_UPDATE},
],
BinarySensorDeviceClass.VIBRATION: [
{CONF_TYPE: CONF_IS_VIBRATION},
{CONF_TYPE: CONF_IS_NO_VIBRATION},
],
BinarySensorDeviceClass.WINDOW: [
{CONF_TYPE: CONF_IS_OPEN},
{CONF_TYPE: CONF_IS_NOT_OPEN},
],
DEVICE_CLASS_NONE: [
{CONF_TYPE: CONF_IS_ON},
{CONF_TYPE: CONF_IS_OFF},
],
}
CONDITION_SCHEMA = cv.DEVICE_CONDITION_BASE_SCHEMA.extend(
{
vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
vol.Required(CONF_TYPE): vol.In(IS_OFF + IS_ON),
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
}
)
async def async_get_conditions(
hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]:
"""List device conditions."""
conditions: list[dict[str, str]] = []
entity_registry = er.async_get(hass)
entries = [
entry
for entry in er.async_entries_for_device(entity_registry, device_id)
if entry.domain == DOMAIN
]
for entry in entries:
device_class = get_device_class(hass, entry.entity_id) or DEVICE_CLASS_NONE
templates = ENTITY_CONDITIONS.get(
device_class, ENTITY_CONDITIONS[DEVICE_CLASS_NONE]
)
conditions.extend(
{
**template,
"condition": "device",
"device_id": device_id,
"entity_id": entry.id,
"domain": DOMAIN,
}
for template in templates
)
return conditions
@callback
def async_condition_from_config(
hass: HomeAssistant, config: ConfigType
) -> condition.ConditionCheckerType:
"""Evaluate state based on configuration."""
condition_type = config[CONF_TYPE]
if condition_type in IS_ON:
stat = "on"
else:
stat = "off"
state_config = {
CONF_CONDITION: "state",
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
CONF_STATE: stat,
}
if CONF_FOR in config:
state_config[CONF_FOR] = config[CONF_FOR]
state_config = cv.STATE_CONDITION_SCHEMA(state_config)
state_config = condition.state_validate_config(hass, state_config)
return condition.state_from_config(state_config)
async def async_get_condition_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List condition capabilities."""
return {
"extra_fields": vol.Schema(
{vol.Optional(CONF_FOR): cv.positive_time_period_dict}
)
}

View File

@@ -0,0 +1,275 @@
"""Provides device triggers for binary sensors."""
import voluptuous as vol
from homeassistant.components.device_automation import (
CONF_TURNED_OFF,
CONF_TURNED_ON,
DEVICE_TRIGGER_BASE_SCHEMA,
)
from homeassistant.components.homeassistant.triggers import state as state_trigger
from homeassistant.const import CONF_ENTITY_ID, CONF_FOR, CONF_TYPE
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.entity import get_device_class
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
from . import DOMAIN, BinarySensorDeviceClass
DEVICE_CLASS_NONE = "none"
CONF_BAT_LOW = "bat_low"
CONF_NOT_BAT_LOW = "not_bat_low"
CONF_CHARGING = "charging"
CONF_NOT_CHARGING = "not_charging"
CONF_CO = "co"
CONF_NO_CO = "no_co"
CONF_COLD = "cold"
CONF_NOT_COLD = "not_cold"
CONF_CONNECTED = "connected"
CONF_NOT_CONNECTED = "not_connected"
CONF_GAS = "gas"
CONF_NO_GAS = "no_gas"
CONF_HOT = "hot"
CONF_NOT_HOT = "not_hot"
CONF_LIGHT = "light"
CONF_NO_LIGHT = "no_light"
CONF_LOCKED = "locked"
CONF_NOT_LOCKED = "not_locked"
CONF_MOIST = "moist"
CONF_NOT_MOIST = "not_moist"
CONF_MOTION = "motion"
CONF_NO_MOTION = "no_motion"
CONF_MOVING = "moving"
CONF_NOT_MOVING = "not_moving"
CONF_OCCUPIED = "occupied"
CONF_NOT_OCCUPIED = "not_occupied"
CONF_PLUGGED_IN = "plugged_in"
CONF_NOT_PLUGGED_IN = "not_plugged_in"
CONF_POWERED = "powered"
CONF_NOT_POWERED = "not_powered"
CONF_PRESENT = "present"
CONF_NOT_PRESENT = "not_present"
CONF_PROBLEM = "problem"
CONF_NO_PROBLEM = "no_problem"
CONF_RUNNING = "running"
CONF_NOT_RUNNING = "not_running"
CONF_UNSAFE = "unsafe"
CONF_NOT_UNSAFE = "not_unsafe"
CONF_SMOKE = "smoke"
CONF_NO_SMOKE = "no_smoke"
CONF_SOUND = "sound"
CONF_NO_SOUND = "no_sound"
CONF_TAMPERED = "tampered"
CONF_NOT_TAMPERED = "not_tampered"
CONF_UPDATE = "update"
CONF_NO_UPDATE = "no_update"
CONF_VIBRATION = "vibration"
CONF_NO_VIBRATION = "no_vibration"
CONF_OPENED = "opened"
CONF_NOT_OPENED = "not_opened"
ENTITY_TRIGGERS = {
BinarySensorDeviceClass.BATTERY: [
{CONF_TYPE: CONF_BAT_LOW},
{CONF_TYPE: CONF_NOT_BAT_LOW},
],
BinarySensorDeviceClass.BATTERY_CHARGING: [
{CONF_TYPE: CONF_CHARGING},
{CONF_TYPE: CONF_NOT_CHARGING},
],
BinarySensorDeviceClass.CO: [
{CONF_TYPE: CONF_CO},
{CONF_TYPE: CONF_NO_CO},
],
BinarySensorDeviceClass.COLD: [
{CONF_TYPE: CONF_COLD},
{CONF_TYPE: CONF_NOT_COLD},
],
BinarySensorDeviceClass.CONNECTIVITY: [
{CONF_TYPE: CONF_CONNECTED},
{CONF_TYPE: CONF_NOT_CONNECTED},
],
BinarySensorDeviceClass.DOOR: [
{CONF_TYPE: CONF_OPENED},
{CONF_TYPE: CONF_NOT_OPENED},
],
BinarySensorDeviceClass.GARAGE_DOOR: [
{CONF_TYPE: CONF_OPENED},
{CONF_TYPE: CONF_NOT_OPENED},
],
BinarySensorDeviceClass.GAS: [
{CONF_TYPE: CONF_GAS},
{CONF_TYPE: CONF_NO_GAS},
],
BinarySensorDeviceClass.HEAT: [
{CONF_TYPE: CONF_HOT},
{CONF_TYPE: CONF_NOT_HOT},
],
BinarySensorDeviceClass.LIGHT: [
{CONF_TYPE: CONF_LIGHT},
{CONF_TYPE: CONF_NO_LIGHT},
],
BinarySensorDeviceClass.LOCK: [
{CONF_TYPE: CONF_NOT_LOCKED},
{CONF_TYPE: CONF_LOCKED},
],
BinarySensorDeviceClass.MOISTURE: [
{CONF_TYPE: CONF_MOIST},
{CONF_TYPE: CONF_NOT_MOIST},
],
BinarySensorDeviceClass.MOTION: [
{CONF_TYPE: CONF_MOTION},
{CONF_TYPE: CONF_NO_MOTION},
],
BinarySensorDeviceClass.MOVING: [
{CONF_TYPE: CONF_MOVING},
{CONF_TYPE: CONF_NOT_MOVING},
],
BinarySensorDeviceClass.OCCUPANCY: [
{CONF_TYPE: CONF_OCCUPIED},
{CONF_TYPE: CONF_NOT_OCCUPIED},
],
BinarySensorDeviceClass.OPENING: [
{CONF_TYPE: CONF_OPENED},
{CONF_TYPE: CONF_NOT_OPENED},
],
BinarySensorDeviceClass.PLUG: [
{CONF_TYPE: CONF_PLUGGED_IN},
{CONF_TYPE: CONF_NOT_PLUGGED_IN},
],
BinarySensorDeviceClass.POWER: [
{CONF_TYPE: CONF_POWERED},
{CONF_TYPE: CONF_NOT_POWERED},
],
BinarySensorDeviceClass.PRESENCE: [
{CONF_TYPE: CONF_PRESENT},
{CONF_TYPE: CONF_NOT_PRESENT},
],
BinarySensorDeviceClass.PROBLEM: [
{CONF_TYPE: CONF_PROBLEM},
{CONF_TYPE: CONF_NO_PROBLEM},
],
BinarySensorDeviceClass.RUNNING: [
{CONF_TYPE: CONF_RUNNING},
{CONF_TYPE: CONF_NOT_RUNNING},
],
BinarySensorDeviceClass.SAFETY: [
{CONF_TYPE: CONF_UNSAFE},
{CONF_TYPE: CONF_NOT_UNSAFE},
],
BinarySensorDeviceClass.SMOKE: [
{CONF_TYPE: CONF_SMOKE},
{CONF_TYPE: CONF_NO_SMOKE},
],
BinarySensorDeviceClass.SOUND: [
{CONF_TYPE: CONF_SOUND},
{CONF_TYPE: CONF_NO_SOUND},
],
BinarySensorDeviceClass.UPDATE: [
{CONF_TYPE: CONF_UPDATE},
{CONF_TYPE: CONF_NO_UPDATE},
],
BinarySensorDeviceClass.TAMPER: [
{CONF_TYPE: CONF_TAMPERED},
{CONF_TYPE: CONF_NOT_TAMPERED},
],
BinarySensorDeviceClass.VIBRATION: [
{CONF_TYPE: CONF_VIBRATION},
{CONF_TYPE: CONF_NO_VIBRATION},
],
BinarySensorDeviceClass.WINDOW: [
{CONF_TYPE: CONF_OPENED},
{CONF_TYPE: CONF_NOT_OPENED},
],
DEVICE_CLASS_NONE: [
{CONF_TYPE: CONF_TURNED_ON},
{CONF_TYPE: CONF_TURNED_OFF},
],
}
TURNED_ON = [trigger[0][CONF_TYPE] for trigger in ENTITY_TRIGGERS.values()]
TURNED_OFF = [trigger[1][CONF_TYPE] for trigger in ENTITY_TRIGGERS.values()]
TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
{
vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
vol.Required(CONF_TYPE): vol.In(TURNED_OFF + TURNED_ON),
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
}
)
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration."""
trigger_type = config[CONF_TYPE]
if trigger_type in TURNED_ON:
to_state = "on"
else:
to_state = "off"
state_config = {
state_trigger.CONF_PLATFORM: "state",
state_trigger.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
state_trigger.CONF_TO: to_state,
}
if CONF_FOR in config:
state_config[CONF_FOR] = config[CONF_FOR]
state_config = await state_trigger.async_validate_trigger_config(hass, state_config)
return await state_trigger.async_attach_trigger(
hass, state_config, action, trigger_info, platform_type="device"
)
async def async_get_triggers(
hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]:
"""List device triggers."""
triggers: list[dict[str, str]] = []
entity_registry = er.async_get(hass)
entries = [
entry
for entry in er.async_entries_for_device(entity_registry, device_id)
if entry.domain == DOMAIN
]
for entry in entries:
device_class = get_device_class(hass, entry.entity_id) or DEVICE_CLASS_NONE
templates = ENTITY_TRIGGERS.get(
device_class, ENTITY_TRIGGERS[DEVICE_CLASS_NONE]
)
triggers.extend(
{
**automation,
"platform": "device",
"device_id": device_id,
"entity_id": entry.id,
"domain": DOMAIN,
}
for automation in templates
)
return triggers
async def async_get_trigger_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List trigger capabilities."""
return {
"extra_fields": vol.Schema(
{vol.Optional(CONF_FOR): cv.positive_time_period_dict}
)
}

View File

@@ -0,0 +1,178 @@
{
"entity_component": {
"_": {
"default": "mdi:radiobox-blank",
"state": {
"on": "mdi:checkbox-marked-circle"
}
},
"battery": {
"default": "mdi:battery",
"state": {
"on": "mdi:battery-outline"
}
},
"battery_charging": {
"default": "mdi:battery",
"state": {
"on": "mdi:battery-charging"
}
},
"carbon_monoxide": {
"default": "mdi:smoke-detector",
"state": {
"on": "mdi:smoke-detector-alert"
}
},
"cold": {
"default": "mdi:thermometer",
"state": {
"on": "mdi:snowflake"
}
},
"connectivity": {
"default": "mdi:close-network-outline",
"state": {
"on": "mdi:check-network-outline"
}
},
"door": {
"default": "mdi:door-closed",
"state": {
"on": "mdi:door-open"
}
},
"garage_door": {
"default": "mdi:garage",
"state": {
"on": "mdi:garage-open"
}
},
"gas": {
"default": "mdi:check-circle",
"state": {
"on": "mdi:alert-circle"
}
},
"heat": {
"default": "mdi:thermometer",
"state": {
"on": "mdi:fire"
}
},
"light": {
"default": "mdi:brightness-5",
"state": {
"on": "mdi:brightness-7"
}
},
"lock": {
"default": "mdi:lock",
"state": {
"on": "mdi:lock-open"
}
},
"moisture": {
"default": "mdi:water-off",
"state": {
"on": "mdi:water"
}
},
"motion": {
"default": "mdi:motion-sensor-off",
"state": {
"on": "mdi:motion-sensor"
}
},
"moving": {
"default": "mdi:arrow-right",
"state": {
"on": "mdi:octagon"
}
},
"occupancy": {
"default": "mdi:home-outline",
"state": {
"on": "mdi:home"
}
},
"opening": {
"default": "mdi:square",
"state": {
"on": "mdi:square-outline"
}
},
"plug": {
"default": "mdi:power-plug-off",
"state": {
"on": "mdi:power-plug"
}
},
"power": {
"default": "mdi:power-plug-off",
"state": {
"on": "mdi:power-plug"
}
},
"presence": {
"default": "mdi:home-outline",
"state": {
"on": "mdi:home"
}
},
"problem": {
"default": "mdi:check-circle",
"state": {
"on": "mdi:alert-circle"
}
},
"running": {
"default": "mdi:stop",
"state": {
"on": "mdi:play"
}
},
"safety": {
"default": "mdi:check-circle",
"state": {
"on": "mdi:alert-circle"
}
},
"smoke": {
"default": "mdi:smoke-detector-variant",
"state": {
"on": "mdi:smoke-detector-variant-alert"
}
},
"sound": {
"default": "mdi:music-note-off",
"state": {
"on": "mdi:music-note"
}
},
"tamper": {
"default": "mdi:check-circle",
"state": {
"on": "mdi:alert-circle"
}
},
"update": {
"default": "mdi:package",
"state": {
"on": "mdi:package-up"
}
},
"vibration": {
"default": "mdi:crop-portrait",
"state": {
"on": "mdi:vibrate"
}
},
"window": {
"default": "mdi:window-closed",
"state": {
"on": "mdi:window-open"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"domain": "binary_sensor",
"name": "Binary Sensor",
"codeowners": ["@home-assistant/core"],
"documentation": "https://www.home-assistant.io/integrations/binary_sensor",
"integration_type": "entity",
"quality_scale": "internal"
}

View File

@@ -0,0 +1,23 @@
"""Helper to test significant Binary Sensor state changes."""
from __future__ import annotations
from typing import Any
from homeassistant.core import HomeAssistant, callback
@callback
def async_check_significant_change(
hass: HomeAssistant,
old_state: str,
old_attrs: dict,
new_state: str,
new_attrs: dict,
**kwargs: Any,
) -> bool | None:
"""Test if state significantly changed."""
if old_state != new_state:
return True
return False

View File

@@ -0,0 +1,321 @@
{
"title": "Binary sensor",
"device_automation": {
"condition_type": {
"is_bat_low": "{entity_name} battery is low",
"is_not_bat_low": "{entity_name} battery is normal",
"is_charging": "{entity_name} is charging",
"is_not_charging": "{entity_name} is not charging",
"is_co": "{entity_name} is detecting carbon monoxide",
"is_no_co": "{entity_name} is not detecting carbon monoxide",
"is_cold": "{entity_name} is cold",
"is_not_cold": "{entity_name} is not cold",
"is_connected": "{entity_name} is connected",
"is_not_connected": "{entity_name} is disconnected",
"is_gas": "{entity_name} is detecting gas",
"is_no_gas": "{entity_name} is not detecting gas",
"is_hot": "{entity_name} is hot",
"is_not_hot": "{entity_name} is not hot",
"is_light": "{entity_name} is detecting light",
"is_no_light": "{entity_name} is not detecting light",
"is_locked": "{entity_name} is locked",
"is_not_locked": "{entity_name} is unlocked",
"is_moist": "{entity_name} is moist",
"is_not_moist": "{entity_name} is dry",
"is_motion": "{entity_name} is detecting motion",
"is_no_motion": "{entity_name} is not detecting motion",
"is_moving": "{entity_name} is moving",
"is_not_moving": "{entity_name} is not moving",
"is_occupied": "{entity_name} is occupied",
"is_not_occupied": "{entity_name} is not occupied",
"is_plugged_in": "{entity_name} is plugged in",
"is_not_plugged_in": "{entity_name} is unplugged",
"is_powered": "{entity_name} is powered",
"is_not_powered": "{entity_name} is not powered",
"is_present": "{entity_name} is present",
"is_not_present": "{entity_name} is not present",
"is_problem": "{entity_name} is detecting problem",
"is_no_problem": "{entity_name} is not detecting problem",
"is_running": "{entity_name} is running",
"is_not_running": "{entity_name} is not running",
"is_unsafe": "{entity_name} is unsafe",
"is_not_unsafe": "{entity_name} is safe",
"is_smoke": "{entity_name} is detecting smoke",
"is_no_smoke": "{entity_name} is not detecting smoke",
"is_sound": "{entity_name} is detecting sound",
"is_no_sound": "{entity_name} is not detecting sound",
"is_tampered": "{entity_name} is detecting tampering",
"is_not_tampered": "{entity_name} is not detecting tampering",
"is_update": "{entity_name} has an update available",
"is_no_update": "{entity_name} is up-to-date",
"is_vibration": "{entity_name} is detecting vibration",
"is_no_vibration": "{entity_name} is not detecting vibration",
"is_open": "{entity_name} is open",
"is_not_open": "{entity_name} is closed",
"is_on": "[%key:common::device_automation::condition_type::is_on%]",
"is_off": "[%key:common::device_automation::condition_type::is_off%]"
},
"extra_fields": {
"for": "[%key:common::device_automation::extra_fields::for%]"
},
"trigger_type": {
"bat_low": "{entity_name} battery low",
"not_bat_low": "{entity_name} battery normal",
"charging": "{entity_name} charging",
"not_charging": "{entity_name} not charging",
"co": "{entity_name} started detecting carbon monoxide",
"no_co": "{entity_name} stopped detecting carbon monoxide",
"cold": "{entity_name} became cold",
"not_cold": "{entity_name} became not cold",
"connected": "{entity_name} connected",
"not_connected": "{entity_name} disconnected",
"gas": "{entity_name} started detecting gas",
"no_gas": "{entity_name} stopped detecting gas",
"hot": "{entity_name} became hot",
"not_hot": "{entity_name} became not hot",
"light": "{entity_name} started detecting light",
"no_light": "{entity_name} stopped detecting light",
"locked": "{entity_name} locked",
"not_locked": "{entity_name} unlocked",
"moist": "{entity_name} became moist",
"not_moist": "{entity_name} became dry",
"motion": "{entity_name} started detecting motion",
"no_motion": "{entity_name} stopped detecting motion",
"moving": "{entity_name} started moving",
"not_moving": "{entity_name} stopped moving",
"occupied": "{entity_name} became occupied",
"not_occupied": "{entity_name} became not occupied",
"plugged_in": "{entity_name} plugged in",
"not_plugged_in": "{entity_name} unplugged",
"powered": "{entity_name} powered",
"not_powered": "{entity_name} not powered",
"present": "{entity_name} present",
"not_present": "{entity_name} not present",
"problem": "{entity_name} started detecting problem",
"no_problem": "{entity_name} stopped detecting problem",
"running": "{entity_name} started running",
"not_running": "{entity_name} is no longer running",
"unsafe": "{entity_name} became unsafe",
"not_unsafe": "{entity_name} became safe",
"smoke": "{entity_name} started detecting smoke",
"no_smoke": "{entity_name} stopped detecting smoke",
"sound": "{entity_name} started detecting sound",
"no_sound": "{entity_name} stopped detecting sound",
"tampered": "{entity_name} started detecting tampering",
"not_tampered": "{entity_name} stopped detecting tampering",
"update": "{entity_name} got an update available",
"no_update": "{entity_name} became up-to-date",
"vibration": "{entity_name} started detecting vibration",
"no_vibration": "{entity_name} stopped detecting vibration",
"opened": "{entity_name} opened",
"not_opened": "{entity_name} closed",
"turned_on": "[%key:common::device_automation::trigger_type::turned_on%]",
"turned_off": "[%key:common::device_automation::trigger_type::turned_off%]"
}
},
"entity_component": {
"_": {
"name": "[%key:component::binary_sensor::title%]",
"state": {
"off": "[%key:common::state::off%]",
"on": "[%key:common::state::on%]"
}
},
"battery": {
"name": "Battery",
"state": {
"off": "Normal",
"on": "Low"
}
},
"battery_charging": {
"name": "Charging",
"state": {
"off": "Not charging",
"on": "Charging"
}
},
"carbon_monoxide": {
"name": "Carbon monoxide",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"cold": {
"name": "Cold",
"state": {
"off": "[%key:component::binary_sensor::entity_component::battery::state::off%]",
"on": "Cold"
}
},
"connectivity": {
"name": "Connectivity",
"state": {
"off": "[%key:common::state::disconnected%]",
"on": "[%key:common::state::connected%]"
}
},
"door": {
"name": "Door",
"state": {
"off": "[%key:common::state::closed%]",
"on": "[%key:common::state::open%]"
}
},
"garage_door": {
"name": "Garage door",
"state": {
"off": "[%key:common::state::closed%]",
"on": "[%key:common::state::open%]"
}
},
"gas": {
"name": "Gas",
"state": {
"off": "Clear",
"on": "Detected"
}
},
"heat": {
"name": "Heat",
"state": {
"off": "[%key:component::binary_sensor::entity_component::battery::state::off%]",
"on": "Hot"
}
},
"light": {
"name": "Light",
"state": {
"off": "No light",
"on": "Light detected"
}
},
"lock": {
"name": "Lock",
"state": {
"off": "[%key:common::state::locked%]",
"on": "[%key:common::state::unlocked%]"
}
},
"moisture": {
"name": "Moisture",
"state": {
"off": "Dry",
"on": "Wet"
}
},
"motion": {
"name": "Motion",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"moving": {
"name": "Moving",
"state": {
"off": "Not moving",
"on": "Moving"
}
},
"occupancy": {
"name": "Occupancy",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"opening": {
"name": "Opening",
"state": {
"off": "[%key:common::state::closed%]",
"on": "[%key:common::state::open%]"
}
},
"plug": {
"name": "Plug",
"state": {
"off": "Unplugged",
"on": "Plugged in"
}
},
"power": {
"name": "Power",
"state": {
"off": "[%key:common::state::off%]",
"on": "[%key:common::state::on%]"
}
},
"presence": {
"name": "Presence",
"state": {
"off": "[%key:common::state::not_home%]",
"on": "[%key:common::state::home%]"
}
},
"problem": {
"name": "Problem",
"state": {
"off": "OK",
"on": "Problem"
}
},
"running": {
"name": "Running",
"state": {
"off": "Not running",
"on": "Running"
}
},
"safety": {
"name": "Safety",
"state": {
"off": "Safe",
"on": "Unsafe"
}
},
"smoke": {
"name": "Smoke",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"sound": {
"name": "Sound",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"tamper": {
"name": "Tamper",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "Tampering detected"
}
},
"update": {
"name": "Update",
"state": {
"off": "Up-to-date",
"on": "Update available"
}
},
"vibration": {
"name": "Vibration",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"window": {
"name": "Window",
"state": {
"off": "[%key:common::state::closed%]",
"on": "[%key:common::state::open%]"
}
}
}
}