2019-04-03 15:40:03 +00:00
|
|
|
"""Provides a binary sensor which gets its values from a TCP socket."""
|
2021-05-22 14:45:18 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any, Final
|
|
|
|
|
2021-05-24 10:03:43 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2021-05-22 14:45:18 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-05-24 10:03:43 +00:00
|
|
|
from .common import TCP_PLATFORM_SCHEMA, TcpEntity
|
2021-05-22 14:45:18 +00:00
|
|
|
from .const import CONF_VALUE_ON
|
2016-02-14 00:03:56 +00:00
|
|
|
|
2021-05-24 10:03:43 +00:00
|
|
|
PLATFORM_SCHEMA: Final = PARENT_PLATFORM_SCHEMA.extend(TCP_PLATFORM_SCHEMA)
|
2016-02-14 00:03:56 +00:00
|
|
|
|
2016-02-22 09:11:46 +00:00
|
|
|
|
2021-05-22 14:45:18 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: dict[str, Any] | None = None,
|
|
|
|
) -> None:
|
2016-10-22 04:14:35 +00:00
|
|
|
"""Set up the TCP binary sensor."""
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([TcpBinarySensor(hass, config)])
|
2016-02-14 00:03:56 +00:00
|
|
|
|
|
|
|
|
2021-05-24 10:03:43 +00:00
|
|
|
class TcpBinarySensor(TcpEntity, BinarySensorEntity):
|
2016-02-22 09:11:46 +00:00
|
|
|
"""A binary sensor which is on when its state == CONF_VALUE_ON."""
|
2016-03-07 19:21:08 +00:00
|
|
|
|
2016-02-14 00:03:56 +00:00
|
|
|
@property
|
2021-05-22 14:45:18 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return true if the binary sensor is on."""
|
2016-02-17 17:26:53 +00:00
|
|
|
return self._state == self._config[CONF_VALUE_ON]
|