1 """Support for HLK-SW16 relay switches."""
5 from hlk_sw16
import create_hlk_sw16_connection
6 import voluptuous
as vol
17 DEFAULT_KEEP_ALIVE_INTERVAL,
19 DEFAULT_RECONNECT_INTERVAL,
23 _LOGGER = logging.getLogger(__name__)
25 PLATFORMS = [Platform.SWITCH]
27 DATA_DEVICE_REGISTER =
"hlk_sw16_device_register"
28 DATA_DEVICE_LISTENER =
"hlk_sw16_device_listener"
30 SWITCH_SCHEMA = vol.Schema({vol.Optional(CONF_NAME): cv.string})
33 vol.Any(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
"a",
"b",
"c",
"d",
"e",
"f"), vol.Coerce(str)
36 CONFIG_SCHEMA = vol.Schema(
40 cv.string: vol.Schema(
42 vol.Required(CONF_HOST): cv.string,
43 vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
44 vol.Required(CONF_SWITCHES): vol.Schema(
45 {RELAY_ID: SWITCH_SCHEMA}
52 extra=vol.ALLOW_EXTRA,
56 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
57 """Component setup, do nothing."""
58 if DOMAIN
not in config:
61 for device_id
in config[DOMAIN]:
62 conf = config[DOMAIN][device_id]
63 hass.async_create_task(
64 hass.config_entries.flow.async_init(
66 context={
"source": SOURCE_IMPORT},
67 data={CONF_HOST: conf[CONF_HOST], CONF_PORT: conf[CONF_PORT]},
74 """Set up the HLK-SW16 switch."""
75 hass.data.setdefault(DOMAIN, {})
76 host = entry.data[CONF_HOST]
77 port = entry.data[CONF_PORT]
78 address = f
"{host}:{port}"
80 hass.data[DOMAIN][entry.entry_id] = {}
84 """Schedule reconnect after connection has been lost."""
85 _LOGGER.warning(
"HLK-SW16 %s disconnected", address)
87 hass, f
"hlk_sw16_device_available_{entry.entry_id}",
False
92 """Schedule reconnect after connection has been lost."""
93 _LOGGER.warning(
"HLK-SW16 %s connected", address)
96 _LOGGER.debug(
"Initiating HLK-SW16 connection to %s", address)
98 client = await create_hlk_sw16_connection(
101 disconnect_callback=disconnected,
102 reconnect_callback=reconnected,
104 timeout=CONNECTION_TIMEOUT,
105 reconnect_interval=DEFAULT_RECONNECT_INTERVAL,
106 keep_alive_interval=DEFAULT_KEEP_ALIVE_INTERVAL,
109 hass.data[DOMAIN][entry.entry_id][DATA_DEVICE_REGISTER] = client
112 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
114 _LOGGER.debug(
"Connected to HLK-SW16 device: %s", address)
120 """Unload a config entry."""
121 client = hass.data[DOMAIN][entry.entry_id].pop(DATA_DEVICE_REGISTER)
123 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
125 if hass.data[DOMAIN][entry.entry_id]:
126 hass.data[DOMAIN].pop(entry.entry_id)
127 if not hass.data[DOMAIN]:
128 hass.data.pop(DOMAIN)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)