Files
homeassistant-core/homeassistant/components/ihc/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
2.4 KiB
Python
Raw Normal View History

2019-02-14 16:01:46 +01:00
"""Support for IHC binary sensors."""
from __future__ import annotations
2022-02-11 10:24:31 +01:00
from ihcsdk.ihccontroller import IHCController
2023-03-28 10:23:00 +02:00
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
2019-02-14 16:01:46 +01:00
from homeassistant.const import CONF_TYPE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2023-03-28 10:23:00 +02:00
from homeassistant.util.enum import try_parse_enum
2018-01-20 16:29:50 +01:00
2022-02-11 10:24:31 +01:00
from .const import CONF_INVERTING, DOMAIN, IHC_CONTROLLER
from .ihcdevice import IHCDevice
2018-01-20 16:29:50 +01:00
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2018-01-20 16:29:50 +01:00
"""Set up the IHC binary sensor platform."""
if discovery_info is None:
return
2018-01-20 16:29:50 +01:00
devices = []
for name, device in discovery_info.items():
ihc_id = device["ihc_id"]
product_cfg = device["product_cfg"]
product = device["product"]
# Find controller that corresponds with device id
2022-02-11 10:24:31 +01:00
controller_id = device["ctrl_id"]
ihc_controller: IHCController = hass.data[DOMAIN][controller_id][IHC_CONTROLLER]
2019-02-14 16:01:46 +01:00
sensor = IHCBinarySensor(
ihc_controller,
2022-02-11 10:24:31 +01:00
controller_id,
2019-02-14 16:01:46 +01:00
name,
ihc_id,
product_cfg.get(CONF_TYPE),
product_cfg[CONF_INVERTING],
product,
)
devices.append(sensor)
2018-08-24 16:37:30 +02:00
add_entities(devices)
2018-01-20 16:29:50 +01:00
class IHCBinarySensor(IHCDevice, BinarySensorEntity):
2018-01-20 16:29:50 +01:00
"""IHC Binary Sensor.
The associated IHC resource can be any in or output from a IHC product
or function block, but it must be a boolean ON/OFF resources.
"""
def __init__(
self,
2022-02-11 10:24:31 +01:00
ihc_controller: IHCController,
controller_id: str,
name: str,
2018-01-20 16:29:50 +01:00
ihc_id: int,
2018-01-29 10:24:08 +02:00
sensor_type: str,
inverting: bool,
product=None,
) -> None:
2018-01-20 16:29:50 +01:00
"""Initialize the IHC binary sensor."""
2022-02-11 10:24:31 +01:00
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
2023-03-28 10:23:00 +02:00
self._attr_device_class = try_parse_enum(BinarySensorDeviceClass, sensor_type)
2018-01-20 16:29:50 +01:00
self.inverting = inverting
def on_ihc_change(self, ihc_id, value):
"""IHC resource has changed."""
if self.inverting:
2023-03-28 10:23:00 +02:00
self._attr_is_on = not value
2018-01-20 16:29:50 +01:00
else:
2023-03-28 10:23:00 +02:00
self._attr_is_on = value
2018-01-20 16:29:50 +01:00
self.schedule_update_ha_state()