2020-08-30 12:16:41 +00:00
|
|
|
"""Binary sensor for Shelly."""
|
2021-07-21 17:11:44 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-24 17:43:43 +00:00
|
|
|
from dataclasses import dataclass
|
2021-08-29 12:52:12 +00:00
|
|
|
from typing import Final, cast
|
2021-07-21 17:11:44 +00:00
|
|
|
|
2020-08-30 12:16:41 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-16 13:12:23 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-08-30 12:16:41 +00:00
|
|
|
BinarySensorEntity,
|
2022-01-24 17:43:43 +00:00
|
|
|
BinarySensorEntityDescription,
|
2020-08-30 12:16:41 +00:00
|
|
|
)
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-05 17:21:20 +00:00
|
|
|
from homeassistant.const import STATE_ON
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-16 13:12:23 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-01-26 14:07:15 +00:00
|
|
|
from homeassistant.helpers.entity_registry import RegistryEntry
|
2020-08-30 12:16:41 +00:00
|
|
|
|
2021-12-13 08:39:13 +00:00
|
|
|
from .const import CONF_SLEEP_PERIOD
|
2020-09-07 12:13:20 +00:00
|
|
|
from .entity import (
|
2022-01-26 14:07:15 +00:00
|
|
|
BlockEntityDescription,
|
2022-01-24 17:43:43 +00:00
|
|
|
RestEntityDescription,
|
2022-01-25 13:48:51 +00:00
|
|
|
RpcEntityDescription,
|
2020-09-07 12:13:20 +00:00
|
|
|
ShellyBlockAttributeEntity,
|
2020-11-11 19:13:14 +00:00
|
|
|
ShellyRestAttributeEntity,
|
2021-09-17 12:53:39 +00:00
|
|
|
ShellyRpcAttributeEntity,
|
2021-02-03 16:03:22 +00:00
|
|
|
ShellySleepingBlockAttributeEntity,
|
2022-10-18 19:42:22 +00:00
|
|
|
ShellySleepingRpcAttributeEntity,
|
2020-09-07 12:13:20 +00:00
|
|
|
async_setup_entry_attribute_entities,
|
2020-11-11 19:13:14 +00:00
|
|
|
async_setup_entry_rest,
|
2021-09-17 12:53:39 +00:00
|
|
|
async_setup_entry_rpc,
|
|
|
|
)
|
|
|
|
from .utils import (
|
|
|
|
get_device_entry_gen,
|
|
|
|
is_block_momentary_input,
|
|
|
|
is_rpc_momentary_input,
|
2020-09-07 12:13:20 +00:00
|
|
|
)
|
2020-08-30 12:16:41 +00:00
|
|
|
|
2022-01-24 17:43:43 +00:00
|
|
|
|
2022-01-26 14:07:15 +00:00
|
|
|
@dataclass
|
|
|
|
class BlockBinarySensorDescription(
|
|
|
|
BlockEntityDescription, BinarySensorEntityDescription
|
|
|
|
):
|
|
|
|
"""Class to describe a BLOCK binary sensor."""
|
|
|
|
|
|
|
|
|
2022-01-25 13:48:51 +00:00
|
|
|
@dataclass
|
|
|
|
class RpcBinarySensorDescription(RpcEntityDescription, BinarySensorEntityDescription):
|
|
|
|
"""Class to describe a RPC binary sensor."""
|
|
|
|
|
|
|
|
|
2022-01-24 17:43:43 +00:00
|
|
|
@dataclass
|
|
|
|
class RestBinarySensorDescription(RestEntityDescription, BinarySensorEntityDescription):
|
|
|
|
"""Class to describe a REST binary sensor."""
|
|
|
|
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
SENSORS: Final = {
|
2022-01-26 14:07:15 +00:00
|
|
|
("device", "overtemp"): BlockBinarySensorDescription(
|
|
|
|
key="device|overtemp",
|
2021-10-14 22:17:00 +00:00
|
|
|
name="Overheating",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("device", "overpower"): BlockBinarySensorDescription(
|
|
|
|
key="device|overpower",
|
2021-10-14 22:17:00 +00:00
|
|
|
name="Overpowering",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("light", "overpower"): BlockBinarySensorDescription(
|
|
|
|
key="light|overpower",
|
2021-10-14 22:17:00 +00:00
|
|
|
name="Overpowering",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("relay", "overpower"): BlockBinarySensorDescription(
|
|
|
|
key="relay|overpower",
|
2021-10-14 22:17:00 +00:00
|
|
|
name="Overpowering",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("sensor", "dwIsOpened"): BlockBinarySensorDescription(
|
|
|
|
key="sensor|dwIsOpened",
|
2021-08-29 12:52:12 +00:00
|
|
|
name="Door",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.OPENING,
|
2021-09-10 21:48:55 +00:00
|
|
|
available=lambda block: cast(int, block.dwIsOpened) != -1,
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("sensor", "flood"): BlockBinarySensorDescription(
|
|
|
|
key="sensor|flood", name="Flood", device_class=BinarySensorDeviceClass.MOISTURE
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("sensor", "gas"): BlockBinarySensorDescription(
|
|
|
|
key="sensor|gas",
|
2020-09-18 12:32:33 +00:00
|
|
|
name="Gas",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.GAS,
|
2020-09-07 12:13:20 +00:00
|
|
|
value=lambda value: value in ["mild", "heavy"],
|
2021-03-11 20:23:20 +00:00
|
|
|
extra_state_attributes=lambda block: {"detected": block.gas},
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("sensor", "smoke"): BlockBinarySensorDescription(
|
|
|
|
key="sensor|smoke", name="Smoke", device_class=BinarySensorDeviceClass.SMOKE
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("sensor", "vibration"): BlockBinarySensorDescription(
|
|
|
|
key="sensor|vibration",
|
|
|
|
name="Vibration",
|
|
|
|
device_class=BinarySensorDeviceClass.VIBRATION,
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("input", "input"): BlockBinarySensorDescription(
|
|
|
|
key="input|input",
|
2020-11-19 10:42:24 +00:00
|
|
|
name="Input",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.POWER,
|
2022-01-26 14:07:15 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-09-17 12:53:39 +00:00
|
|
|
removal_condition=is_block_momentary_input,
|
2020-11-19 10:42:24 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("relay", "input"): BlockBinarySensorDescription(
|
|
|
|
key="relay|input",
|
2020-11-19 10:42:24 +00:00
|
|
|
name="Input",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.POWER,
|
2022-01-26 14:07:15 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-09-17 12:53:39 +00:00
|
|
|
removal_condition=is_block_momentary_input,
|
2020-11-19 10:42:24 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("device", "input"): BlockBinarySensorDescription(
|
|
|
|
key="device|input",
|
2020-11-19 10:42:24 +00:00
|
|
|
name="Input",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.POWER,
|
2022-01-26 14:07:15 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-09-17 12:53:39 +00:00
|
|
|
removal_condition=is_block_momentary_input,
|
2020-11-19 10:42:24 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("sensor", "extInput"): BlockBinarySensorDescription(
|
|
|
|
key="sensor|extInput",
|
2021-02-25 08:51:18 +00:00
|
|
|
name="External Input",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.POWER,
|
2022-01-26 14:07:15 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-02-25 08:51:18 +00:00
|
|
|
),
|
2022-01-26 14:07:15 +00:00
|
|
|
("sensor", "motion"): BlockBinarySensorDescription(
|
|
|
|
key="sensor|motion", name="Motion", device_class=BinarySensorDeviceClass.MOTION
|
2020-12-30 23:02:56 +00:00
|
|
|
),
|
2020-08-30 12:16:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
REST_SENSORS: Final = {
|
2022-01-24 17:43:43 +00:00
|
|
|
"cloud": RestBinarySensorDescription(
|
|
|
|
key="cloud",
|
2020-11-11 19:13:14 +00:00
|
|
|
name="Cloud",
|
2020-11-27 08:40:06 +00:00
|
|
|
value=lambda status, _: status["cloud"]["connected"],
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
2022-01-24 17:43:43 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-12-16 13:12:23 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2020-11-11 19:13:14 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
RPC_SENSORS: Final = {
|
2022-01-25 13:48:51 +00:00
|
|
|
"input": RpcBinarySensorDescription(
|
2021-09-17 12:53:39 +00:00
|
|
|
key="input",
|
2021-10-04 20:46:46 +00:00
|
|
|
sub_key="state",
|
2021-09-17 12:53:39 +00:00
|
|
|
name="Input",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.POWER,
|
2022-01-25 13:48:51 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-09-17 12:53:39 +00:00
|
|
|
removal_condition=is_rpc_momentary_input,
|
|
|
|
),
|
2022-01-25 13:48:51 +00:00
|
|
|
"cloud": RpcBinarySensorDescription(
|
2021-09-17 12:53:39 +00:00
|
|
|
key="cloud",
|
2021-10-04 20:46:46 +00:00
|
|
|
sub_key="connected",
|
2021-09-17 12:53:39 +00:00
|
|
|
name="Cloud",
|
2021-12-16 13:12:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
2022-01-25 13:48:51 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-12-16 13:12:23 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-09-17 12:53:39 +00:00
|
|
|
),
|
2022-02-19 16:51:01 +00:00
|
|
|
"overtemp": RpcBinarySensorDescription(
|
|
|
|
key="switch",
|
|
|
|
sub_key="errors",
|
|
|
|
name="Overheating",
|
|
|
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
|
|
value=lambda status, _: False if status is None else "overtemp" in status,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
supported=lambda status: status.get("apower") is not None,
|
|
|
|
),
|
|
|
|
"overpower": RpcBinarySensorDescription(
|
|
|
|
key="switch",
|
|
|
|
sub_key="errors",
|
|
|
|
name="Overpowering",
|
|
|
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
|
|
value=lambda status, _: False if status is None else "overpower" in status,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
supported=lambda status: status.get("apower") is not None,
|
|
|
|
),
|
|
|
|
"overvoltage": RpcBinarySensorDescription(
|
|
|
|
key="switch",
|
|
|
|
sub_key="errors",
|
|
|
|
name="Overvoltage",
|
|
|
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
|
|
value=lambda status, _: False if status is None else "overvoltage" in status,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
supported=lambda status: status.get("apower") is not None,
|
|
|
|
),
|
2023-01-12 07:42:40 +00:00
|
|
|
"smoke": RpcBinarySensorDescription(
|
|
|
|
key="smoke",
|
|
|
|
sub_key="alarm",
|
|
|
|
name="Smoke",
|
|
|
|
device_class=BinarySensorDeviceClass.SMOKE,
|
|
|
|
),
|
2021-09-17 12:53:39 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 12:16:41 +00:00
|
|
|
|
2022-01-26 14:07:15 +00:00
|
|
|
def _build_block_description(entry: RegistryEntry) -> BlockBinarySensorDescription:
|
|
|
|
"""Build description when restoring block attribute entities."""
|
|
|
|
return BlockBinarySensorDescription(
|
|
|
|
key="",
|
|
|
|
name="",
|
|
|
|
icon=entry.original_icon,
|
|
|
|
device_class=entry.original_device_class,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-08-30 12:16:41 +00:00
|
|
|
"""Set up sensors for device."""
|
2021-09-17 12:53:39 +00:00
|
|
|
if get_device_entry_gen(config_entry) == 2:
|
2022-10-18 19:42:22 +00:00
|
|
|
if config_entry.data[CONF_SLEEP_PERIOD]:
|
|
|
|
async_setup_entry_rpc(
|
|
|
|
hass,
|
|
|
|
config_entry,
|
|
|
|
async_add_entities,
|
|
|
|
RPC_SENSORS,
|
|
|
|
RpcSleepingBinarySensor,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
async_setup_entry_rpc(
|
|
|
|
hass, config_entry, async_add_entities, RPC_SENSORS, RpcBinarySensor
|
|
|
|
)
|
|
|
|
return
|
2021-09-17 12:53:39 +00:00
|
|
|
|
2021-11-06 15:32:58 +00:00
|
|
|
if config_entry.data[CONF_SLEEP_PERIOD]:
|
2022-05-17 17:57:41 +00:00
|
|
|
async_setup_entry_attribute_entities(
|
2021-02-03 16:03:22 +00:00
|
|
|
hass,
|
|
|
|
config_entry,
|
|
|
|
async_add_entities,
|
|
|
|
SENSORS,
|
2021-09-17 12:53:39 +00:00
|
|
|
BlockSleepingBinarySensor,
|
2022-01-26 14:07:15 +00:00
|
|
|
_build_block_description,
|
2021-02-03 16:03:22 +00:00
|
|
|
)
|
|
|
|
else:
|
2022-05-17 17:57:41 +00:00
|
|
|
async_setup_entry_attribute_entities(
|
2022-01-26 14:07:15 +00:00
|
|
|
hass,
|
|
|
|
config_entry,
|
|
|
|
async_add_entities,
|
|
|
|
SENSORS,
|
|
|
|
BlockBinarySensor,
|
|
|
|
_build_block_description,
|
2021-02-03 16:03:22 +00:00
|
|
|
)
|
2022-05-17 17:57:41 +00:00
|
|
|
async_setup_entry_rest(
|
2021-02-03 16:03:22 +00:00
|
|
|
hass,
|
|
|
|
config_entry,
|
|
|
|
async_add_entities,
|
|
|
|
REST_SENSORS,
|
2021-09-17 12:53:39 +00:00
|
|
|
RestBinarySensor,
|
2021-02-03 16:03:22 +00:00
|
|
|
)
|
2020-11-11 19:13:14 +00:00
|
|
|
|
2020-08-30 12:16:41 +00:00
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
class BlockBinarySensor(ShellyBlockAttributeEntity, BinarySensorEntity):
|
|
|
|
"""Represent a block binary sensor entity."""
|
2020-08-30 12:16:41 +00:00
|
|
|
|
2022-01-26 14:07:15 +00:00
|
|
|
entity_description: BlockBinarySensorDescription
|
|
|
|
|
2020-08-30 12:16:41 +00:00
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def is_on(self) -> bool:
|
2020-09-03 08:54:25 +00:00
|
|
|
"""Return true if sensor state is on."""
|
2020-09-07 12:13:20 +00:00
|
|
|
return bool(self.attribute_value)
|
2020-11-11 19:13:14 +00:00
|
|
|
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
class RestBinarySensor(ShellyRestAttributeEntity, BinarySensorEntity):
|
|
|
|
"""Represent a REST binary sensor entity."""
|
2020-11-11 19:13:14 +00:00
|
|
|
|
2022-01-24 17:43:43 +00:00
|
|
|
entity_description: RestBinarySensorDescription
|
|
|
|
|
2020-11-11 19:13:14 +00:00
|
|
|
@property
|
2022-11-19 16:07:02 +00:00
|
|
|
def is_on(self) -> bool:
|
2020-11-11 19:13:14 +00:00
|
|
|
"""Return true if REST sensor state is on."""
|
|
|
|
return bool(self.attribute_value)
|
2021-02-03 16:03:22 +00:00
|
|
|
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
class RpcBinarySensor(ShellyRpcAttributeEntity, BinarySensorEntity):
|
|
|
|
"""Represent a RPC binary sensor entity."""
|
|
|
|
|
2022-01-25 13:48:51 +00:00
|
|
|
entity_description: RpcBinarySensorDescription
|
|
|
|
|
2021-09-17 12:53:39 +00:00
|
|
|
@property
|
2022-11-19 16:07:02 +00:00
|
|
|
def is_on(self) -> bool:
|
2021-09-17 12:53:39 +00:00
|
|
|
"""Return true if RPC sensor state is on."""
|
|
|
|
return bool(self.attribute_value)
|
|
|
|
|
|
|
|
|
|
|
|
class BlockSleepingBinarySensor(ShellySleepingBlockAttributeEntity, BinarySensorEntity):
|
|
|
|
"""Represent a block sleeping binary sensor."""
|
2021-02-03 16:03:22 +00:00
|
|
|
|
2022-01-26 14:07:15 +00:00
|
|
|
entity_description: BlockBinarySensorDescription
|
|
|
|
|
2021-02-03 16:03:22 +00:00
|
|
|
@property
|
2021-07-21 17:11:44 +00:00
|
|
|
def is_on(self) -> bool:
|
2021-02-03 16:03:22 +00:00
|
|
|
"""Return true if sensor state is on."""
|
|
|
|
if self.block is not None:
|
|
|
|
return bool(self.attribute_value)
|
|
|
|
|
|
|
|
return self.last_state == STATE_ON
|
2022-10-18 19:42:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RpcSleepingBinarySensor(ShellySleepingRpcAttributeEntity, BinarySensorEntity):
|
|
|
|
"""Represent a RPC sleeping binary sensor entity."""
|
|
|
|
|
|
|
|
entity_description: RpcBinarySensorDescription
|
|
|
|
|
|
|
|
@property
|
2022-11-19 16:07:02 +00:00
|
|
|
def is_on(self) -> bool:
|
2022-10-18 19:42:22 +00:00
|
|
|
"""Return true if RPC sensor state is on."""
|
|
|
|
if self.coordinator.device.initialized:
|
|
|
|
return bool(self.attribute_value)
|
|
|
|
|
|
|
|
return self.last_state == STATE_ON
|