2020-08-30 12:16:41 +00:00
|
|
|
"""Binary sensor for Shelly."""
|
2021-07-21 17:11:44 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Final
|
|
|
|
|
2020-08-30 12:16:41 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2020-11-11 19:13:14 +00:00
|
|
|
DEVICE_CLASS_CONNECTIVITY,
|
2020-09-03 08:54:25 +00:00
|
|
|
DEVICE_CLASS_GAS,
|
2020-08-30 12:16:41 +00:00
|
|
|
DEVICE_CLASS_MOISTURE,
|
2020-12-30 23:02:56 +00:00
|
|
|
DEVICE_CLASS_MOTION,
|
2020-08-30 12:16:41 +00:00
|
|
|
DEVICE_CLASS_OPENING,
|
2020-11-19 10:42:24 +00:00
|
|
|
DEVICE_CLASS_POWER,
|
2020-09-08 21:22:44 +00:00
|
|
|
DEVICE_CLASS_PROBLEM,
|
2020-08-30 12:16:41 +00:00
|
|
|
DEVICE_CLASS_SMOKE,
|
2020-09-02 06:56:27 +00:00
|
|
|
DEVICE_CLASS_VIBRATION,
|
2021-02-03 16:03:22 +00:00
|
|
|
STATE_ON,
|
2020-08-30 12:16:41 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2021-07-21 17:11:44 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-08-30 12:16:41 +00:00
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
from .entity import (
|
|
|
|
BlockAttributeDescription,
|
2020-11-11 19:13:14 +00:00
|
|
|
RestAttributeDescription,
|
2020-09-07 12:13:20 +00:00
|
|
|
ShellyBlockAttributeEntity,
|
2020-11-11 19:13:14 +00:00
|
|
|
ShellyRestAttributeEntity,
|
2021-02-03 16:03:22 +00:00
|
|
|
ShellySleepingBlockAttributeEntity,
|
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,
|
2020-09-07 12:13:20 +00:00
|
|
|
)
|
2020-11-19 10:42:24 +00:00
|
|
|
from .utils import is_momentary_input
|
2020-08-30 12:16:41 +00:00
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
SENSORS: Final = {
|
2020-09-08 21:22:44 +00:00
|
|
|
("device", "overtemp"): BlockAttributeDescription(
|
|
|
|
name="Overheating", device_class=DEVICE_CLASS_PROBLEM
|
|
|
|
),
|
|
|
|
("device", "overpower"): BlockAttributeDescription(
|
2020-09-18 12:32:33 +00:00
|
|
|
name="Overpowering", device_class=DEVICE_CLASS_PROBLEM
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
|
|
|
("light", "overpower"): BlockAttributeDescription(
|
2020-09-18 12:32:33 +00:00
|
|
|
name="Overpowering", device_class=DEVICE_CLASS_PROBLEM
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
|
|
|
("relay", "overpower"): BlockAttributeDescription(
|
2020-09-18 12:32:33 +00:00
|
|
|
name="Overpowering", device_class=DEVICE_CLASS_PROBLEM
|
2020-09-08 21:22:44 +00:00
|
|
|
),
|
2020-09-07 12:13:20 +00:00
|
|
|
("sensor", "dwIsOpened"): BlockAttributeDescription(
|
|
|
|
name="Door", device_class=DEVICE_CLASS_OPENING
|
|
|
|
),
|
|
|
|
("sensor", "flood"): BlockAttributeDescription(
|
2020-09-18 12:32:33 +00:00
|
|
|
name="Flood", device_class=DEVICE_CLASS_MOISTURE
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
|
|
|
("sensor", "gas"): BlockAttributeDescription(
|
2020-09-18 12:32:33 +00:00
|
|
|
name="Gas",
|
2020-09-07 12:13:20 +00:00
|
|
|
device_class=DEVICE_CLASS_GAS,
|
|
|
|
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
|
|
|
),
|
|
|
|
("sensor", "smoke"): BlockAttributeDescription(
|
2020-09-18 12:32:33 +00:00
|
|
|
name="Smoke", device_class=DEVICE_CLASS_SMOKE
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
|
|
|
("sensor", "vibration"): BlockAttributeDescription(
|
2020-09-18 12:32:33 +00:00
|
|
|
name="Vibration", device_class=DEVICE_CLASS_VIBRATION
|
2020-09-07 12:13:20 +00:00
|
|
|
),
|
2020-11-19 10:42:24 +00:00
|
|
|
("input", "input"): BlockAttributeDescription(
|
|
|
|
name="Input",
|
|
|
|
device_class=DEVICE_CLASS_POWER,
|
|
|
|
default_enabled=False,
|
|
|
|
removal_condition=is_momentary_input,
|
|
|
|
),
|
|
|
|
("relay", "input"): BlockAttributeDescription(
|
|
|
|
name="Input",
|
|
|
|
device_class=DEVICE_CLASS_POWER,
|
|
|
|
default_enabled=False,
|
|
|
|
removal_condition=is_momentary_input,
|
|
|
|
),
|
|
|
|
("device", "input"): BlockAttributeDescription(
|
|
|
|
name="Input",
|
|
|
|
device_class=DEVICE_CLASS_POWER,
|
|
|
|
default_enabled=False,
|
|
|
|
removal_condition=is_momentary_input,
|
|
|
|
),
|
2021-02-25 08:51:18 +00:00
|
|
|
("sensor", "extInput"): BlockAttributeDescription(
|
|
|
|
name="External Input",
|
|
|
|
device_class=DEVICE_CLASS_POWER,
|
|
|
|
default_enabled=False,
|
|
|
|
),
|
2020-12-30 23:02:56 +00:00
|
|
|
("sensor", "motion"): BlockAttributeDescription(
|
|
|
|
name="Motion", device_class=DEVICE_CLASS_MOTION
|
|
|
|
),
|
2020-08-30 12:16:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 17:11:44 +00:00
|
|
|
REST_SENSORS: Final = {
|
2020-11-11 19:13:14 +00:00
|
|
|
"cloud": RestAttributeDescription(
|
|
|
|
name="Cloud",
|
2020-11-27 08:40:06 +00:00
|
|
|
value=lambda status, _: status["cloud"]["connected"],
|
2020-11-11 19:13:14 +00:00
|
|
|
device_class=DEVICE_CLASS_CONNECTIVITY,
|
|
|
|
default_enabled=False,
|
|
|
|
),
|
|
|
|
"fwupdate": RestAttributeDescription(
|
2021-02-25 08:51:18 +00:00
|
|
|
name="Firmware Update",
|
2020-11-11 19:13:14 +00:00
|
|
|
icon="mdi:update",
|
2020-11-27 08:40:06 +00:00
|
|
|
value=lambda status, _: status["update"]["has_update"],
|
2020-11-11 19:13:14 +00:00
|
|
|
default_enabled=False,
|
2021-03-11 20:23:20 +00:00
|
|
|
extra_state_attributes=lambda status: {
|
2020-11-27 08:40:06 +00:00
|
|
|
"latest_stable_version": status["update"]["new_version"],
|
|
|
|
"installed_version": status["update"]["old_version"],
|
|
|
|
},
|
2020-11-11 19:13:14 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2020-08-30 12:16:41 +00:00
|
|
|
|
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-02-03 16:03:22 +00:00
|
|
|
if config_entry.data["sleep_period"]:
|
|
|
|
await async_setup_entry_attribute_entities(
|
|
|
|
hass,
|
|
|
|
config_entry,
|
|
|
|
async_add_entities,
|
|
|
|
SENSORS,
|
|
|
|
ShellySleepingBinarySensor,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await async_setup_entry_attribute_entities(
|
|
|
|
hass, config_entry, async_add_entities, SENSORS, ShellyBinarySensor
|
|
|
|
)
|
|
|
|
await async_setup_entry_rest(
|
|
|
|
hass,
|
|
|
|
config_entry,
|
|
|
|
async_add_entities,
|
|
|
|
REST_SENSORS,
|
|
|
|
ShellyRestBinarySensor,
|
|
|
|
)
|
2020-11-11 19:13:14 +00:00
|
|
|
|
2020-08-30 12:16:41 +00:00
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
class ShellyBinarySensor(ShellyBlockAttributeEntity, BinarySensorEntity):
|
|
|
|
"""Shelly binary sensor entity."""
|
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
|
|
|
|
|
|
|
|
|
|
|
class ShellyRestBinarySensor(ShellyRestAttributeEntity, BinarySensorEntity):
|
|
|
|
"""Shelly REST binary sensor entity."""
|
|
|
|
|
|
|
|
@property
|
2021-07-21 17:11:44 +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
|
|
|
|
|
|
|
|
|
|
|
class ShellySleepingBinarySensor(
|
|
|
|
ShellySleepingBlockAttributeEntity, BinarySensorEntity
|
|
|
|
):
|
|
|
|
"""Represent a shelly sleeping binary sensor."""
|
|
|
|
|
|
|
|
@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
|