1 """The WiLight Device integration."""
3 from __future__
import annotations
9 from pywilight.wilight_device
import PyWiLightDevice
17 _LOGGER = logging.getLogger(__name__)
21 """Manages a single WiLight Parent Device."""
23 def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) ->
None:
24 """Initialize the system."""
25 self._host: str = config_entry.data[CONF_HOST]
27 self.
_api_api: PyWiLightDevice |
None =
None
31 """Return the host of this parent."""
35 def api(self) -> PyWiLightDevice | None:
36 """Return the api of this parent."""
40 """Set up a WiLight Parent Device based on host parameter."""
42 hass = self.
_hass_hass
44 api_device = await hass.async_add_executor_job(create_api_device, host)
46 if api_device
is None:
50 def disconnected() -> None:
52 _LOGGER.warning(
"WiLight %s disconnected", api_device.device_id)
54 hass, f
"wilight_device_available_{api_device.device_id}",
False
58 def reconnected() -> None:
60 _LOGGER.warning(
"WiLight %s reconnect", api_device.device_id)
62 hass, f
"wilight_device_available_{api_device.device_id}",
True
65 async
def connect(api_device: PyWiLightDevice) ->
None:
67 _LOGGER.debug(
"Initiating connection to %s", api_device.device_id)
69 client = await api_device.config_client(
70 disconnect_callback=disconnected,
71 reconnect_callback=reconnected,
72 loop=asyncio.get_running_loop(),
77 hass.bus.async_listen_once(
78 EVENT_HOMEASSISTANT_STOP,
lambda x: client.stop()
81 _LOGGER.debug(
"Connected to WiLight device: %s", api_device.device_id)
83 await connect(api_device)
93 if self.
_api_api
is not None:
94 self.
_api_api.client.stop()
98 """Create an API Device."""
100 return pywilight.device_from_host(host)
102 requests.exceptions.ConnectionError,
103 requests.exceptions.Timeout,
105 _LOGGER.error(
"Unable to access WiLight at %s (%s)", host, err)
PyWiLightDevice|None api(self)
None __init__(self, HomeAssistant hass, ConfigEntry config_entry)
PyWiLightDevice create_api_device(str host)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)