2019-02-19 13:09:06 +00:00
|
|
|
"""Support for INSTEON Modems (PLM and Hub)."""
|
2018-02-25 19:13:39 +00:00
|
|
|
import logging
|
2018-09-10 14:54:17 +00:00
|
|
|
|
2019-10-21 08:22:34 +00:00
|
|
|
import insteonplm
|
2017-02-21 07:53:39 +00:00
|
|
|
|
2019-02-19 13:09:06 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PLATFORM,
|
|
|
|
CONF_PORT,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
|
|
|
)
|
2018-09-10 14:54:17 +00:00
|
|
|
|
2020-01-30 09:47:44 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_CAT,
|
|
|
|
CONF_DIM_STEPS,
|
|
|
|
CONF_FIRMWARE,
|
|
|
|
CONF_HOUSECODE,
|
|
|
|
CONF_HUB_PASSWORD,
|
|
|
|
CONF_HUB_USERNAME,
|
|
|
|
CONF_HUB_VERSION,
|
|
|
|
CONF_IP_PORT,
|
|
|
|
CONF_OVERRIDE,
|
|
|
|
CONF_PRODUCT_KEY,
|
|
|
|
CONF_SUBCAT,
|
|
|
|
CONF_UNITCODE,
|
|
|
|
CONF_X10,
|
|
|
|
CONF_X10_ALL_LIGHTS_OFF,
|
|
|
|
CONF_X10_ALL_LIGHTS_ON,
|
|
|
|
CONF_X10_ALL_UNITS_OFF,
|
|
|
|
DOMAIN,
|
|
|
|
INSTEON_ENTITIES,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-30 09:47:44 +00:00
|
|
|
from .schemas import CONFIG_SCHEMA # noqa F440
|
|
|
|
from .utils import async_register_services, register_new_device_callback
|
2017-02-21 07:53:39 +00:00
|
|
|
|
2020-01-30 09:47:44 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-12-14 13:02:06 +00:00
|
|
|
|
2017-02-21 07:53:39 +00:00
|
|
|
|
2018-10-01 06:52:42 +00:00
|
|
|
async def async_setup(hass, config):
|
2018-08-22 07:09:04 +00:00
|
|
|
"""Set up the connection to the modem."""
|
|
|
|
insteon_modem = None
|
2018-02-25 19:13:39 +00:00
|
|
|
|
2017-02-21 07:53:39 +00:00
|
|
|
conf = config[DOMAIN]
|
|
|
|
port = conf.get(CONF_PORT)
|
2018-08-22 07:09:04 +00:00
|
|
|
host = conf.get(CONF_HOST)
|
|
|
|
ip_port = conf.get(CONF_IP_PORT)
|
|
|
|
username = conf.get(CONF_HUB_USERNAME)
|
|
|
|
password = conf.get(CONF_HUB_PASSWORD)
|
2018-09-10 14:54:17 +00:00
|
|
|
hub_version = conf.get(CONF_HUB_VERSION)
|
2018-02-25 19:13:39 +00:00
|
|
|
overrides = conf.get(CONF_OVERRIDE, [])
|
2018-06-21 01:44:05 +00:00
|
|
|
x10_devices = conf.get(CONF_X10, [])
|
|
|
|
x10_all_units_off_housecode = conf.get(CONF_X10_ALL_UNITS_OFF)
|
|
|
|
x10_all_lights_on_housecode = conf.get(CONF_X10_ALL_LIGHTS_ON)
|
|
|
|
x10_all_lights_off_housecode = conf.get(CONF_X10_ALL_LIGHTS_OFF)
|
2017-02-21 07:53:39 +00:00
|
|
|
|
2018-08-22 07:09:04 +00:00
|
|
|
if host:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info("Connecting to Insteon Hub on %s", host)
|
2018-10-01 06:52:42 +00:00
|
|
|
conn = await insteonplm.Connection.create(
|
2018-08-22 07:09:04 +00:00
|
|
|
host=host,
|
|
|
|
port=ip_port,
|
|
|
|
username=username,
|
|
|
|
password=password,
|
2018-09-10 14:54:17 +00:00
|
|
|
hub_version=hub_version,
|
2018-08-22 07:09:04 +00:00
|
|
|
loop=hass.loop,
|
2019-07-31 19:25:30 +00:00
|
|
|
workdir=hass.config.config_dir,
|
|
|
|
)
|
2018-08-22 07:09:04 +00:00
|
|
|
else:
|
|
|
|
_LOGGER.info("Looking for Insteon PLM on %s", port)
|
2018-10-01 06:52:42 +00:00
|
|
|
conn = await insteonplm.Connection.create(
|
2019-07-31 19:25:30 +00:00
|
|
|
device=port, loop=hass.loop, workdir=hass.config.config_dir
|
|
|
|
)
|
2018-08-22 07:09:04 +00:00
|
|
|
|
|
|
|
insteon_modem = conn.protocol
|
2017-02-21 07:53:39 +00:00
|
|
|
|
2020-01-30 09:47:44 +00:00
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
hass.data[DOMAIN]["modem"] = insteon_modem
|
|
|
|
hass.data[DOMAIN][INSTEON_ENTITIES] = set()
|
|
|
|
|
|
|
|
register_new_device_callback(hass, config, insteon_modem)
|
|
|
|
async_register_services(hass, config, insteon_modem)
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, conn.close)
|
|
|
|
|
2018-02-25 19:13:39 +00:00
|
|
|
for device_override in overrides:
|
2017-02-21 07:53:39 +00:00
|
|
|
#
|
|
|
|
# Override the device default capabilities for a specific address
|
|
|
|
#
|
2019-07-31 19:25:30 +00:00
|
|
|
address = device_override.get("address")
|
2018-02-25 19:13:39 +00:00
|
|
|
for prop in device_override:
|
|
|
|
if prop in [CONF_CAT, CONF_SUBCAT]:
|
2019-07-31 19:25:30 +00:00
|
|
|
insteon_modem.devices.add_override(address, prop, device_override[prop])
|
2018-02-25 19:13:39 +00:00
|
|
|
elif prop in [CONF_FIRMWARE, CONF_PRODUCT_KEY]:
|
2019-07-31 19:25:30 +00:00
|
|
|
insteon_modem.devices.add_override(
|
|
|
|
address, CONF_PRODUCT_KEY, device_override[prop]
|
|
|
|
)
|
2017-02-21 07:53:39 +00:00
|
|
|
|
2018-06-21 01:44:05 +00:00
|
|
|
if x10_all_units_off_housecode:
|
2019-07-31 19:25:30 +00:00
|
|
|
device = insteon_modem.add_x10_device(
|
|
|
|
x10_all_units_off_housecode, 20, "allunitsoff"
|
|
|
|
)
|
2018-06-21 01:44:05 +00:00
|
|
|
if x10_all_lights_on_housecode:
|
2019-07-31 19:25:30 +00:00
|
|
|
device = insteon_modem.add_x10_device(
|
|
|
|
x10_all_lights_on_housecode, 21, "alllightson"
|
|
|
|
)
|
2018-06-21 01:44:05 +00:00
|
|
|
if x10_all_lights_off_housecode:
|
2019-07-31 19:25:30 +00:00
|
|
|
device = insteon_modem.add_x10_device(
|
|
|
|
x10_all_lights_off_housecode, 22, "alllightsoff"
|
|
|
|
)
|
2018-06-21 01:44:05 +00:00
|
|
|
for device in x10_devices:
|
|
|
|
housecode = device.get(CONF_HOUSECODE)
|
|
|
|
unitcode = device.get(CONF_UNITCODE)
|
2019-07-31 19:25:30 +00:00
|
|
|
x10_type = "onoff"
|
2018-06-21 01:44:05 +00:00
|
|
|
steps = device.get(CONF_DIM_STEPS, 22)
|
2019-07-31 19:25:30 +00:00
|
|
|
if device.get(CONF_PLATFORM) == "light":
|
|
|
|
x10_type = "dimmable"
|
|
|
|
elif device.get(CONF_PLATFORM) == "binary_sensor":
|
|
|
|
x10_type = "sensor"
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Adding X10 device to Insteon: %s %d %s", housecode, unitcode, x10_type
|
|
|
|
)
|
|
|
|
device = insteon_modem.add_x10_device(housecode, unitcode, x10_type)
|
|
|
|
if device and hasattr(device.states[0x01], "steps"):
|
2018-06-21 01:44:05 +00:00
|
|
|
device.states[0x01].steps = steps
|
|
|
|
|
2017-02-21 07:53:39 +00:00
|
|
|
return True
|