2019-02-13 20:21:14 +00:00
|
|
|
"""Support for binary sensors through the SmartThings cloud API."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-20 15:40:41 +00:00
|
|
|
from collections.abc import Sequence
|
2019-02-15 16:40:54 +00:00
|
|
|
|
2019-07-01 02:29:21 +00:00
|
|
|
from pysmartthings import Attribute, Capability
|
|
|
|
|
2020-09-12 00:37:33 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-20 08:46:33 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 00:37:33 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2022-01-03 12:13:03 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-20 08:46:33 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2022-01-03 12:13:03 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-02-02 22:06:30 +00:00
|
|
|
|
|
|
|
from . import SmartThingsEntity
|
|
|
|
from .const import DATA_BROKERS, DOMAIN
|
|
|
|
|
|
|
|
CAPABILITY_TO_ATTRIB = {
|
2019-07-01 02:29:21 +00:00
|
|
|
Capability.acceleration_sensor: Attribute.acceleration,
|
|
|
|
Capability.contact_sensor: Attribute.contact,
|
|
|
|
Capability.filter_status: Attribute.filter_status,
|
|
|
|
Capability.motion_sensor: Attribute.motion,
|
|
|
|
Capability.presence_sensor: Attribute.presence,
|
|
|
|
Capability.sound_sensor: Attribute.sound,
|
|
|
|
Capability.tamper_alert: Attribute.tamper,
|
|
|
|
Capability.valve: Attribute.valve,
|
|
|
|
Capability.water_sensor: Attribute.water,
|
2019-02-02 22:06:30 +00:00
|
|
|
}
|
|
|
|
ATTRIB_TO_CLASS = {
|
2021-12-20 08:46:33 +00:00
|
|
|
Attribute.acceleration: BinarySensorDeviceClass.MOVING,
|
|
|
|
Attribute.contact: BinarySensorDeviceClass.OPENING,
|
|
|
|
Attribute.filter_status: BinarySensorDeviceClass.PROBLEM,
|
|
|
|
Attribute.motion: BinarySensorDeviceClass.MOTION,
|
|
|
|
Attribute.presence: BinarySensorDeviceClass.PRESENCE,
|
|
|
|
Attribute.sound: BinarySensorDeviceClass.SOUND,
|
|
|
|
Attribute.tamper: BinarySensorDeviceClass.PROBLEM,
|
|
|
|
Attribute.valve: BinarySensorDeviceClass.OPENING,
|
|
|
|
Attribute.water: BinarySensorDeviceClass.MOISTURE,
|
2019-02-02 22:06:30 +00:00
|
|
|
}
|
2021-10-26 12:55:03 +00:00
|
|
|
ATTRIB_TO_ENTTIY_CATEGORY = {
|
2021-12-20 08:46:33 +00:00
|
|
|
Attribute.tamper: EntityCategory.DIAGNOSTIC,
|
2021-10-26 12:55:03 +00:00
|
|
|
}
|
2019-02-02 22:06:30 +00:00
|
|
|
|
|
|
|
|
2022-01-03 12:13:03 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-02-02 22:06:30 +00:00
|
|
|
"""Add binary sensors for a config entry."""
|
|
|
|
broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id]
|
|
|
|
sensors = []
|
|
|
|
for device in broker.devices.values():
|
2019-07-31 19:25:30 +00:00
|
|
|
for capability in broker.get_assigned(device.device_id, "binary_sensor"):
|
2019-02-15 16:40:54 +00:00
|
|
|
attrib = CAPABILITY_TO_ATTRIB[capability]
|
|
|
|
sensors.append(SmartThingsBinarySensor(device, attrib))
|
2019-02-02 22:06:30 +00:00
|
|
|
async_add_entities(sensors)
|
|
|
|
|
|
|
|
|
2021-03-18 13:31:38 +00:00
|
|
|
def get_capabilities(capabilities: Sequence[str]) -> Sequence[str] | None:
|
2019-02-15 16:40:54 +00:00
|
|
|
"""Return all capabilities supported if minimum required are present."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return [
|
|
|
|
capability for capability in CAPABILITY_TO_ATTRIB if capability in capabilities
|
|
|
|
]
|
2019-02-15 16:40:54 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class SmartThingsBinarySensor(SmartThingsEntity, BinarySensorEntity):
|
2019-02-02 22:06:30 +00:00
|
|
|
"""Define a SmartThings Binary Sensor."""
|
|
|
|
|
|
|
|
def __init__(self, device, attribute):
|
|
|
|
"""Init the class."""
|
|
|
|
super().__init__(device)
|
|
|
|
self._attribute = attribute
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the binary sensor."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{self._device.label} {self._attribute}"
|
2019-02-02 22:06:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique ID."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{self._device.device_id}.{self._attribute}"
|
2019-02-02 22:06:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the binary sensor is on."""
|
|
|
|
return self._device.status.is_on(self._attribute)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this device."""
|
|
|
|
return ATTRIB_TO_CLASS[self._attribute]
|
2021-10-26 12:55:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_category(self):
|
|
|
|
"""Return the entity category of this device."""
|
|
|
|
return ATTRIB_TO_ENTTIY_CATEGORY.get(self._attribute)
|