hass-core/homeassistant/components/wilight/parent_device.py
Leonardo Figueiro 34984a8af8
Add switch to wilight ()
* Created switch.py and support

* updated support.py

* test for wilight switch

* Update for Test

* Updated test_switch.py

* Trigger service with index

* Updated support.py and switch.py

* Updated support.py

* Updated switch.py as PR#63614

* Updated switch.py

* add type hints

* Updated support.py

* Updated switch.py

* Updated switch.py and services.yaml

* Updated pywilight

* Update homeassistant/components/wilight/switch.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/wilight/switch.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/wilight/switch.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/wilight/switch.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update ci.yaml

* Update ci.yaml

* Updated as pywilight

Renamed Device as PyWiLightDevice in pywilight.

* Updated as pywilight

Renamed Device as PyWiLightDevice in pywilight.

* Updated as pywilight

Renamed Device as PyWiLightDevice in pywilight.

* Updated as pywilight

Renamed Device as PyWiLightDevice in pywilight.

* Update switch.py

* Update homeassistant/components/wilight/support.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update support.py

* Update switch.py

* Update support.py

* Update support.py

* Update switch.py

* Update switch.py

* Update services.yaml

* Update switch.py

* Update services.yaml

* Update switch.py

* Update homeassistant/components/wilight/switch.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/wilight/switch.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/wilight/switch.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update switch.py

* Update switch.py

* Update switch.py

* Update test_switch.py

* Update test_switch.py

* Update test_switch.py

* Decrease exception scope

* Clean up

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2022-08-07 11:07:01 +02:00

105 lines
3.3 KiB
Python

"""The WiLight Device integration."""
from __future__ import annotations
import asyncio
import logging
import pywilight
from pywilight.wilight_device import PyWiLightDevice
import requests
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_send
_LOGGER = logging.getLogger(__name__)
class WiLightParent:
"""Manages a single WiLight Parent Device."""
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Initialize the system."""
self._host: str = config_entry.data[CONF_HOST]
self._hass = hass
self._api: PyWiLightDevice | None = None
@property
def host(self) -> str:
"""Return the host of this parent."""
return self._host
@property
def api(self) -> PyWiLightDevice | None:
"""Return the api of this parent."""
return self._api
async def async_setup(self) -> bool:
"""Set up a WiLight Parent Device based on host parameter."""
host = self._host
hass = self._hass
api_device = await hass.async_add_executor_job(create_api_device, host)
if api_device is None:
return False
@callback
def disconnected() -> None:
# Schedule reconnect after connection has been lost.
_LOGGER.warning("WiLight %s disconnected", api_device.device_id)
async_dispatcher_send(
hass, f"wilight_device_available_{api_device.device_id}", False
)
@callback
def reconnected() -> None:
# Schedule reconnect after connection has been lost.
_LOGGER.warning("WiLight %s reconnect", api_device.device_id)
async_dispatcher_send(
hass, f"wilight_device_available_{api_device.device_id}", True
)
async def connect(api_device: PyWiLightDevice) -> None:
# Set up connection and hook it into HA for reconnect/shutdown.
_LOGGER.debug("Initiating connection to %s", api_device.device_id)
client = await api_device.config_client(
disconnect_callback=disconnected,
reconnect_callback=reconnected,
loop=asyncio.get_running_loop(),
logger=_LOGGER,
)
# handle shutdown of WiLight asyncio transport
hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, lambda x: client.stop()
)
_LOGGER.info("Connected to WiLight device: %s", api_device.device_id)
await connect(api_device)
self._api = api_device
return True
async def async_reset(self) -> None:
"""Reset api."""
# If the initialization was not wrong.
if self._api is not None:
self._api.client.stop()
def create_api_device(host: str) -> PyWiLightDevice:
"""Create an API Device."""
try:
return pywilight.device_from_host(host)
except (
requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
) as err:
_LOGGER.error("Unable to access WiLight at %s (%s)", host, err)
return None