Files
core/homeassistant/components/rest/binary_sensor.py
T

135 lines
3.9 KiB
Python
Raw Normal View History

2019-04-03 17:40:03 +02:00
"""Support for RESTful binary sensors."""
from __future__ import annotations
import voluptuous as vol
2016-08-21 01:28:45 +02:00
from homeassistant.components.binary_sensor import (
DOMAIN as BINARY_SENSOR_DOMAIN,
2019-07-31 12:25:30 -07:00
PLATFORM_SCHEMA,
2020-06-24 14:53:17 +02:00
BinarySensorEntity,
2019-07-31 12:25:30 -07:00
)
2016-08-21 01:28:45 +02:00
from homeassistant.const import (
2019-07-31 12:25:30 -07:00
CONF_DEVICE_CLASS,
CONF_FORCE_UPDATE,
2019-07-31 12:25:30 -07:00
CONF_RESOURCE,
CONF_RESOURCE_TEMPLATE,
CONF_UNIQUE_ID,
2019-07-31 12:25:30 -07:00
CONF_VALUE_TEMPLATE,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.template_entity import TemplateEntity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import async_get_config_and_coordinator, create_rest_data_from_config
from .const import DEFAULT_BINARY_SENSOR_NAME
from .entity import RestEntity
from .schema import BINARY_SENSOR_SCHEMA, RESOURCE_SCHEMA
2016-08-21 01:28:45 +02:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({**RESOURCE_SCHEMA, **BINARY_SENSOR_SCHEMA})
2015-12-17 00:47:12 +01:00
PLATFORM_SCHEMA = vol.All(
cv.has_at_least_one_key(CONF_RESOURCE, CONF_RESOURCE_TEMPLATE), PLATFORM_SCHEMA
)
2015-12-17 00:47:12 +01:00
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2017-04-30 07:04:49 +02:00
"""Set up the REST binary sensor."""
# Must update the sensor now (including fetching the rest resource) to
# ensure it's updating its state.
if discovery_info is not None:
conf, coordinator, rest = await async_get_config_and_coordinator(
hass, BINARY_SENSOR_DOMAIN, discovery_info
)
else:
conf = config
coordinator = None
rest = create_rest_data_from_config(hass, conf)
await rest.async_update(log_errors=False)
2016-01-02 13:29:33 -08:00
if rest.data is None:
if rest.last_exception:
raise PlatformNotReady from rest.last_exception
raise PlatformNotReady
2015-12-17 00:47:12 +01:00
unique_id = conf.get(CONF_UNIQUE_ID)
async_add_entities(
[
RestBinarySensor(
hass,
coordinator,
rest,
conf,
unique_id,
)
],
)
2015-12-17 00:47:12 +01:00
class RestBinarySensor(RestEntity, TemplateEntity, BinarySensorEntity):
2016-03-25 18:34:58 +01:00
"""Representation of a REST binary sensor."""
2015-12-17 00:47:12 +01:00
def __init__(
self,
hass,
coordinator,
rest,
config,
unique_id,
):
"""Initialize a REST binary sensor."""
RestEntity.__init__(
self,
coordinator,
rest,
config.get(CONF_RESOURCE_TEMPLATE),
config.get(CONF_FORCE_UPDATE),
)
TemplateEntity.__init__(
self,
hass,
config=config,
fallback_name=DEFAULT_BINARY_SENSOR_NAME,
unique_id=unique_id,
)
2015-12-17 00:47:12 +01:00
self._state = False
2016-06-18 18:48:32 +02:00
self._previous_data = None
self._value_template = config.get(CONF_VALUE_TEMPLATE)
if (value_template := self._value_template) is not None:
value_template.hass = hass
self._is_on = None
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
2021-10-22 22:48:13 +02:00
2015-12-17 00:47:12 +01:00
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._is_on
def _update_from_rest_data(self):
"""Update state from the rest data."""
2016-01-02 13:29:33 -08:00
if self.rest.data is None:
self._is_on = False
2016-01-02 13:29:33 -08:00
response = self.rest.data
2016-01-02 13:29:33 -08:00
if self._value_template is not None:
2019-07-31 12:25:30 -07:00
response = self._value_template.async_render_with_possible_json_value(
self.rest.data, False
)
2016-06-18 18:48:32 +02:00
try:
self._is_on = bool(int(response))
2016-06-18 18:48:32 +02:00
except ValueError:
self._is_on = {"true": True, "on": True, "open": True, "yes": True}.get(
2019-07-31 12:25:30 -07:00
response.lower(), False
)