|
2 | 2 | import logging
|
3 | 3 |
|
4 | 4 | import voluptuous as vol
|
| 5 | +from async_timeout import timeout |
| 6 | + |
| 7 | +from aiohttp.client_exceptions import ClientConnectorError |
5 | 8 |
|
6 | 9 | from fullykiosk import FullyKiosk
|
| 10 | +from fullykiosk.exceptions import FullyKioskError |
7 | 11 |
|
8 | 12 | from homeassistant import config_entries, core, exceptions
|
9 | 13 | from homeassistant.const import CONF_HOST, CONF_PORT, CONF_PASSWORD
|
| 14 | +from homeassistant.helpers.aiohttp_client import async_get_clientsession |
| 15 | + |
10 | 16 |
|
11 | 17 | from .const import DOMAIN # pylint:disable=unused-import
|
12 | 18 |
|
@@ -39,25 +45,15 @@ async def authenticate(self, username, password) -> bool:
|
39 | 45 |
|
40 | 46 |
|
41 | 47 | async def validate_input(hass: core.HomeAssistant, data):
|
42 |
| - """Validate the user input allows us to connect. |
43 |
| -
|
44 |
| - Data has the keys from DATA_SCHEMA with values provided by the user. |
45 |
| - """ |
46 |
| - # TODO validate the data can be used to set up a connection. |
47 |
| - |
48 |
| - # If your PyPI package is not built with async, pass your methods |
49 |
| - # to the executor: |
50 |
| - # await hass.async_add_executor_job( |
51 |
| - # your_validate_func, data["username"], data["password"] |
52 |
| - # ) |
53 |
| - |
54 |
| - fully = FullyKiosk(data["host"], data["port"], data["password"]) |
55 |
| - |
56 |
| - # try: |
57 |
| - deviceInfo = await hass.async_add_executor_job(fully.getDeviceInfo) |
58 |
| - # except: |
59 |
| - |
60 |
| - # raise InvalidAuth |
| 48 | + """Validate the user input allows us to connect.""" |
| 49 | + session = async_get_clientsession() |
| 50 | + fully = FullyKiosk(session, data["host"], data["port"], data["password"]) |
| 51 | + |
| 52 | + try: |
| 53 | + with timeout(10): |
| 54 | + deviceInfo = await fully.getDeviceInfo() |
| 55 | + except (FullyKioskError, ClientConnectorError): |
| 56 | + raise CannotConnect |
61 | 57 |
|
62 | 58 | # If you cannot connect:
|
63 | 59 | # throw CannotConnect
|
|
0 commit comments