1 """Support for RESTful binary sensors."""
3 from __future__
import annotations
7 from xml.parsers.expat
import ExpatError
9 import voluptuous
as vol
12 DOMAIN
as BINARY_SENSOR_DOMAIN,
13 PLATFORM_SCHEMA
as BINARY_SENSOR_PLATFORM_SCHEMA,
22 CONF_RESOURCE_TEMPLATE,
39 from .
import async_get_config_and_coordinator, create_rest_data_from_config
40 from .const
import DEFAULT_BINARY_SENSOR_NAME
41 from .data
import RestData
42 from .entity
import RestEntity
43 from .schema
import BINARY_SENSOR_SCHEMA, RESOURCE_SCHEMA
45 _LOGGER = logging.getLogger(__name__)
47 PLATFORM_SCHEMA = vol.All(
48 BINARY_SENSOR_PLATFORM_SCHEMA.extend({**RESOURCE_SCHEMA, **BINARY_SENSOR_SCHEMA}),
49 cv.has_at_least_one_key(CONF_RESOURCE, CONF_RESOURCE_TEMPLATE),
52 TRIGGER_ENTITY_OPTIONS = (
64 async_add_entities: AddEntitiesCallback,
65 discovery_info: DiscoveryInfoType |
None =
None,
67 """Set up the REST binary sensor."""
70 if discovery_info
is not None:
72 hass, BINARY_SENSOR_DOMAIN, discovery_info
78 await rest.async_update(log_errors=
False)
81 if rest.last_exception:
82 if isinstance(rest.last_exception, ssl.SSLError):
84 "Error connecting %s failed with %s",
89 raise PlatformNotReady
from rest.last_exception
90 raise PlatformNotReady
92 name = conf.get(CONF_NAME)
or Template(DEFAULT_BINARY_SENSOR_NAME, hass)
94 trigger_entity_config = {CONF_NAME: name}
96 for key
in TRIGGER_ENTITY_OPTIONS:
99 trigger_entity_config[key] = conf[key]
108 trigger_entity_config,
115 """Representation of a REST binary sensor."""
120 coordinator: DataUpdateCoordinator[
None] |
None,
123 trigger_entity_config: ConfigType,
125 """Initialize a REST binary sensor."""
126 ManualTriggerEntity.__init__(self, hass, trigger_entity_config)
131 config.get(CONF_RESOURCE_TEMPLATE),
132 config[CONF_FORCE_UPDATE],
135 self._value_template: Template |
None = config.get(CONF_VALUE_TEMPLATE)
139 """Return if entity is available."""
140 available1 = RestEntity.available.fget(self)
141 available2 = ManualTriggerEntity.available.fget(self)
142 return bool(available1
and available2)
145 """Update state from the rest data."""
146 if self.
restrest.data
is None:
151 response = self.
restrest.data_without_xml()
152 except ExpatError
as err:
155 "REST xml result could not be parsed and converted to JSON: %s", err
161 if response
is not None and self._value_template
is not None:
162 response = self._value_template.async_render_with_possible_json_value(
174 }.
get(
str(response).lower(),
False)
None __init__(self, HomeAssistant hass, DataUpdateCoordinator[None]|None coordinator, RestData rest, ConfigType config, ConfigType trigger_entity_config)
None _update_from_rest_data(self)
None async_write_ha_state(self)
None _process_manual_data(self, Any|None value=None)
web.Response get(self, web.Request request, str config_key)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
RestData create_rest_data_from_config(HomeAssistant hass, ConfigType config)
tuple[ConfigType, DataUpdateCoordinator[None], RestData] async_get_config_and_coordinator(HomeAssistant hass, str platform_domain, DiscoveryInfoType discovery_info)