130 lines
3.9 KiB
Python
130 lines
3.9 KiB
Python
"""Support for HLK-SW16 relay switches."""
|
|
|
|
import logging
|
|
|
|
from hlk_sw16 import create_hlk_sw16_connection
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SWITCHES, Platform
|
|
from homeassistant.core import HomeAssistant, callback
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
from .const import (
|
|
CONNECTION_TIMEOUT,
|
|
DEFAULT_KEEP_ALIVE_INTERVAL,
|
|
DEFAULT_PORT,
|
|
DEFAULT_RECONNECT_INTERVAL,
|
|
DOMAIN,
|
|
)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
PLATFORMS = [Platform.SWITCH]
|
|
|
|
DATA_DEVICE_REGISTER = "hlk_sw16_device_register"
|
|
DATA_DEVICE_LISTENER = "hlk_sw16_device_listener"
|
|
|
|
SWITCH_SCHEMA = vol.Schema({vol.Optional(CONF_NAME): cv.string})
|
|
|
|
RELAY_ID = vol.All(
|
|
vol.Any(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f"), vol.Coerce(str)
|
|
)
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
{
|
|
DOMAIN: vol.Schema(
|
|
{
|
|
cv.string: vol.Schema(
|
|
{
|
|
vol.Required(CONF_HOST): cv.string,
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
vol.Required(CONF_SWITCHES): vol.Schema(
|
|
{RELAY_ID: SWITCH_SCHEMA}
|
|
),
|
|
}
|
|
)
|
|
}
|
|
)
|
|
},
|
|
extra=vol.ALLOW_EXTRA,
|
|
)
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
"""Component setup, do nothing."""
|
|
if DOMAIN not in config:
|
|
return True
|
|
|
|
for device_id in config[DOMAIN]:
|
|
conf = config[DOMAIN][device_id]
|
|
hass.async_create_task(
|
|
hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_IMPORT},
|
|
data={CONF_HOST: conf[CONF_HOST], CONF_PORT: conf[CONF_PORT]},
|
|
)
|
|
)
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Set up the HLK-SW16 switch."""
|
|
hass.data.setdefault(DOMAIN, {})
|
|
host = entry.data[CONF_HOST]
|
|
port = entry.data[CONF_PORT]
|
|
address = f"{host}:{port}"
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {}
|
|
|
|
@callback
|
|
def disconnected():
|
|
"""Schedule reconnect after connection has been lost."""
|
|
_LOGGER.warning("HLK-SW16 %s disconnected", address)
|
|
async_dispatcher_send(
|
|
hass, f"hlk_sw16_device_available_{entry.entry_id}", False
|
|
)
|
|
|
|
@callback
|
|
def reconnected():
|
|
"""Schedule reconnect after connection has been lost."""
|
|
_LOGGER.warning("HLK-SW16 %s connected", address)
|
|
async_dispatcher_send(hass, f"hlk_sw16_device_available_{entry.entry_id}", True)
|
|
|
|
_LOGGER.debug("Initiating HLK-SW16 connection to %s", address)
|
|
|
|
client = await create_hlk_sw16_connection(
|
|
host=host,
|
|
port=port,
|
|
disconnect_callback=disconnected,
|
|
reconnect_callback=reconnected,
|
|
loop=hass.loop,
|
|
timeout=CONNECTION_TIMEOUT,
|
|
reconnect_interval=DEFAULT_RECONNECT_INTERVAL,
|
|
keep_alive_interval=DEFAULT_KEEP_ALIVE_INTERVAL,
|
|
)
|
|
|
|
hass.data[DOMAIN][entry.entry_id][DATA_DEVICE_REGISTER] = client
|
|
|
|
# Load entities
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
_LOGGER.debug("Connected to HLK-SW16 device: %s", address)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
client = hass.data[DOMAIN][entry.entry_id].pop(DATA_DEVICE_REGISTER)
|
|
client.stop()
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
if unload_ok:
|
|
if hass.data[DOMAIN][entry.entry_id]:
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
if not hass.data[DOMAIN]:
|
|
hass.data.pop(DOMAIN)
|
|
return unload_ok
|