core/homeassistant/components/demo/binary_sensor.py

72 lines
1.9 KiB
Python
Raw Normal View History

"""Demo platform that has two fake binary sensors."""
from __future__ import annotations
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DOMAIN
2015-11-19 18:00:22 +00:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the demo binary sensor platform."""
async_add_entities(
2019-07-31 19:25:30 +00:00
[
DemoBinarySensor(
"binary_1",
"Basement Floor Wet",
False,
BinarySensorDeviceClass.MOISTURE,
),
DemoBinarySensor(
"binary_2", "Movement Backyard", True, BinarySensorDeviceClass.MOTION
),
2019-07-31 19:25:30 +00:00
]
)
2015-11-19 18:00:22 +00:00
class DemoBinarySensor(BinarySensorEntity):
"""representation of a Demo binary sensor."""
2016-03-07 19:21:08 +00:00
_attr_has_entity_name = True
_attr_name = None
_attr_should_poll = False
def __init__(
self,
unique_id: str,
device_name: str,
state: bool,
device_class: BinarySensorDeviceClass,
) -> None:
2016-03-07 19:21:08 +00:00
"""Initialize the demo sensor."""
self._unique_id = unique_id
2015-11-19 18:00:22 +00:00
self._state = state
self._attr_device_class = device_class
self._attr_device_info = DeviceInfo(
2021-10-22 15:00:00 +00:00
identifiers={
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self.unique_id)
},
name=device_name,
2021-10-22 15:00:00 +00:00
)
@property
2022-06-30 13:34:48 +00:00
def unique_id(self) -> str:
"""Return the unique id."""
return self._unique_id
2015-11-19 18:00:22 +00:00
@property
2022-06-30 13:34:48 +00:00
def is_on(self) -> bool:
"""Return true if the binary sensor is on."""
2015-11-19 18:00:22 +00:00
return self._state