1
mirror of https://github.com/home-assistant/core synced 2024-09-18 19:55:20 +02:00
ha-core/homeassistant/components/bosch_shc/binary_sensor.py
2021-07-18 15:10:42 -05:00

92 lines
3.1 KiB
Python

"""Platform for binarysensor integration."""
from boschshcpy import SHCBatteryDevice, SHCSession, SHCShutterContact
from boschshcpy.device import SHCDevice
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_DOOR,
DEVICE_CLASS_WINDOW,
BinarySensorEntity,
)
from .const import DATA_SESSION, DOMAIN
from .entity import SHCEntity
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the SHC binary sensor platform."""
entities = []
session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
for binary_sensor in session.device_helper.shutter_contacts:
entities.append(
ShutterContactSensor(
device=binary_sensor,
parent_id=session.information.unique_id,
entry_id=config_entry.entry_id,
)
)
for binary_sensor in (
session.device_helper.motion_detectors
+ session.device_helper.shutter_contacts
+ session.device_helper.smoke_detectors
+ session.device_helper.thermostats
+ session.device_helper.twinguards
+ session.device_helper.universal_switches
+ session.device_helper.wallthermostats
+ session.device_helper.water_leakage_detectors
):
if binary_sensor.supports_batterylevel:
entities.append(
BatterySensor(
device=binary_sensor,
parent_id=session.information.unique_id,
entry_id=config_entry.entry_id,
)
)
if entities:
async_add_entities(entities)
class ShutterContactSensor(SHCEntity, BinarySensorEntity):
"""Representation of an SHC shutter contact sensor."""
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC shutter contact sensor.."""
super().__init__(device, parent_id, entry_id)
switcher = {
"ENTRANCE_DOOR": DEVICE_CLASS_DOOR,
"REGULAR_WINDOW": DEVICE_CLASS_WINDOW,
"FRENCH_WINDOW": DEVICE_CLASS_DOOR,
"GENERIC": DEVICE_CLASS_WINDOW,
}
self._attr_device_class = switcher.get(
self._device.device_class, DEVICE_CLASS_WINDOW
)
@property
def is_on(self):
"""Return the state of the sensor."""
return self._device.state == SHCShutterContact.ShutterContactService.State.OPEN
class BatterySensor(SHCEntity, BinarySensorEntity):
"""Representation of an SHC battery reporting sensor."""
_attr_device_class = DEVICE_CLASS_BATTERY
def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
"""Initialize an SHC battery reporting sensor."""
super().__init__(device, parent_id, entry_id)
self._attr_name = f"{device.name} Battery"
self._attr_unique_id = f"{device.serial}_battery"
@property
def is_on(self):
"""Return the state of the sensor."""
return (
self._device.batterylevel != SHCBatteryDevice.BatteryLevelService.State.OK
)