1 """Support for an exposed aREST RESTful API of a device."""
3 from __future__
import annotations
5 from http
import HTTPStatus
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SWITCH_PLATFORM_SCHEMA,
22 _LOGGER = logging.getLogger(__name__)
24 CONF_FUNCTIONS =
"functions"
26 CONF_INVERT =
"invert"
28 DEFAULT_NAME =
"aREST switch"
30 PIN_FUNCTION_SCHEMA = vol.Schema(
32 vol.Optional(CONF_NAME): cv.string,
33 vol.Optional(CONF_INVERT, default=
False): cv.boolean,
37 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
39 vol.Required(CONF_RESOURCE): cv.url,
40 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
41 vol.Optional(CONF_PINS, default={}): vol.Schema(
42 {cv.string: PIN_FUNCTION_SCHEMA}
44 vol.Optional(CONF_FUNCTIONS, default={}): vol.Schema(
45 {cv.string: PIN_FUNCTION_SCHEMA}
54 add_entities: AddEntitiesCallback,
55 discovery_info: DiscoveryInfoType |
None =
None,
57 """Set up the aREST switches."""
58 resource = config[CONF_RESOURCE]
61 response = requests.get(resource, timeout=10)
62 except requests.exceptions.MissingSchema:
64 "Missing resource or schema in configuration. Add http:// to your URL"
67 except requests.exceptions.ConnectionError:
68 _LOGGER.error(
"No route to device at %s", resource)
71 dev: list[SwitchEntity] = []
72 pins = config[CONF_PINS]
73 for pinnum, pin
in pins.items():
77 config.get(CONF_NAME, response.json()[CONF_NAME]),
84 functions = config[CONF_FUNCTIONS]
85 for funcname, func
in functions.items():
89 config.get(CONF_NAME, response.json()[CONF_NAME]),
99 """Representation of an aREST switch."""
102 """Initialize the switch."""
104 self.
_attr_name_attr_name = f
"{location.title()} {name.title()}"
110 """Representation of an aREST switch."""
112 def __init__(self, resource, location, name, func):
113 """Initialize the switch."""
114 super().
__init__(resource, location, name)
117 request = requests.get(f
"{self._resource}/{self._func}", timeout=10)
119 if request.status_code != HTTPStatus.OK:
120 _LOGGER.error(
"Can't find function")
124 request.json()[
"return_value"]
126 _LOGGER.error(
"No return_value received")
128 _LOGGER.error(
"Response invalid")
131 """Turn the device on."""
132 request = requests.get(
133 f
"{self._resource}/{self._func}", timeout=10, params={
"params":
"1"}
136 if request.status_code == HTTPStatus.OK:
139 _LOGGER.error(
"Can't turn on function %s at %s", self.
_func_func, self.
_resource_resource)
142 """Turn the device off."""
143 request = requests.get(
144 f
"{self._resource}/{self._func}", timeout=10, params={
"params":
"0"}
147 if request.status_code == HTTPStatus.OK:
151 "Can't turn off function %s at %s", self.
_func_func, self.
_resource_resource
155 """Get the latest data from aREST API and update the state."""
157 request = requests.get(f
"{self._resource}/{self._func}", timeout=10)
160 except requests.exceptions.ConnectionError:
161 _LOGGER.warning(
"No route to device %s", self.
_resource_resource)
166 """Representation of an aREST switch. Based on digital I/O."""
168 def __init__(self, resource, location, name, pin, invert) -> None:
169 """Initialize the switch."""
170 super().
__init__(resource, location, name)
177 """Turn the device on."""
178 turn_on_payload =
int(
not self.
invertinvert)
179 request = requests.get(
180 f
"{self._resource}/digital/{self._pin}/{turn_on_payload}", timeout=10
182 if request.status_code == HTTPStatus.OK:
185 _LOGGER.error(
"Can't turn on pin %s at %s", self.
_pin_pin, self.
_resource_resource)
188 """Turn the device off."""
189 turn_off_payload =
int(self.
invertinvert)
190 request = requests.get(
191 f
"{self._resource}/digital/{self._pin}/{turn_off_payload}", timeout=10
193 if request.status_code == HTTPStatus.OK:
196 _LOGGER.error(
"Can't turn off pin %s at %s", self.
_pin_pin, self.
_resource_resource)
199 """Get the latest data from aREST API and update the state."""
201 request = requests.get(f
"{self._resource}/digital/{self._pin}", timeout=10)
207 except requests.exceptions.ConnectionError:
208 _LOGGER.warning(
"No route to device %s", self.
_resource_resource)
212 request = requests.get(f
"{self._resource}/mode/{self._pin}/o", timeout=10)
213 if request.status_code != HTTPStatus.OK:
214 _LOGGER.error(
"Can't set mode")
def __init__(self, resource, location, name)
None turn_off(self, **Any kwargs)
None turn_on(self, **Any kwargs)
def __init__(self, resource, location, name, func)
None __set_pin_output(self)
None turn_off(self, **Any kwargs)
None turn_on(self, **Any kwargs)
None __init__(self, resource, location, name, pin, invert)
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)