1 """Support for an exposed aREST RESTful API of a device."""
3 from __future__
import annotations
5 from datetime
import timedelta
6 from http
import HTTPStatus
10 import voluptuous
as vol
13 DEVICE_CLASSES_SCHEMA,
14 PLATFORM_SCHEMA
as BINARY_SENSOR_PLATFORM_SCHEMA,
24 _LOGGER = logging.getLogger(__name__)
28 PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
30 vol.Required(CONF_RESOURCE): cv.url,
31 vol.Optional(CONF_NAME): cv.string,
32 vol.Required(CONF_PIN): cv.string,
33 vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
41 add_entities: AddEntitiesCallback,
42 discovery_info: DiscoveryInfoType |
None =
None,
44 """Set up the aREST binary sensor."""
45 resource = config[CONF_RESOURCE]
46 pin = config[CONF_PIN]
47 device_class = config.get(CONF_DEVICE_CLASS)
50 response = requests.get(resource, timeout=10).
json()
51 except requests.exceptions.MissingSchema:
53 "Missing resource or schema in configuration. Add http:// to your URL"
56 except requests.exceptions.ConnectionError:
57 _LOGGER.error(
"No route to device at %s", resource)
67 config.get(CONF_NAME, response[CONF_NAME]),
77 """Implement an aREST binary sensor for a pin."""
79 def __init__(self, arest, resource, name, device_class, pin):
80 """Initialize the aREST device."""
86 request = requests.get(f
"{resource}/mode/{pin}/i", timeout=10)
87 if request.status_code != HTTPStatus.OK:
88 _LOGGER.error(
"Can't set mode of %s", resource)
91 """Get the latest data from aREST API."""
97 """Class for handling the data retrieval for pins."""
100 """Initialize the aREST data object."""
105 @Throttle(MIN_TIME_BETWEEN_UPDATES)
107 """Get the latest data from aREST device."""
109 response = requests.get(f
"{self._resource}/digital/{self._pin}", timeout=10)
110 self.
datadata = {
"state": response.json()[
"return_value"]}
111 except requests.exceptions.ConnectionError:
112 _LOGGER.error(
"No route to device '%s'", self.
_resource_resource)
def __init__(self, arest, resource, name, device_class, pin)
def __init__(self, resource, pin)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)