This commit is contained in:
Paulus Schoutsen 2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View file

@ -8,33 +8,43 @@ import sys
import async_timeout
import voluptuous as vol
from homeassistant.const import (
CONF_OPTIMISTIC, EVENT_HOMEASSISTANT_STOP)
from homeassistant.const import CONF_OPTIMISTIC, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.setup import async_setup_component
from .const import (
CONF_BAUD_RATE, CONF_DEVICE, CONF_GATEWAYS, CONF_NODES,
CONF_PERSISTENCE, CONF_PERSISTENCE_FILE, CONF_RETAIN, CONF_TCP_PORT,
CONF_TOPIC_IN_PREFIX, CONF_TOPIC_OUT_PREFIX, CONF_VERSION, DOMAIN,
MYSENSORS_GATEWAY_READY, MYSENSORS_GATEWAYS)
CONF_BAUD_RATE,
CONF_DEVICE,
CONF_GATEWAYS,
CONF_NODES,
CONF_PERSISTENCE,
CONF_PERSISTENCE_FILE,
CONF_RETAIN,
CONF_TCP_PORT,
CONF_TOPIC_IN_PREFIX,
CONF_TOPIC_OUT_PREFIX,
CONF_VERSION,
DOMAIN,
MYSENSORS_GATEWAY_READY,
MYSENSORS_GATEWAYS,
)
from .handler import HANDLERS
from .helpers import discover_mysensors_platform, validate_child, validate_node
_LOGGER = logging.getLogger(__name__)
GATEWAY_READY_TIMEOUT = 15.0
MQTT_COMPONENT = 'mqtt'
MQTT_COMPONENT = "mqtt"
def is_serial_port(value):
"""Validate that value is a windows serial port or a unix device."""
if sys.platform.startswith('win'):
ports = ('COM{}'.format(idx + 1) for idx in range(256))
if sys.platform.startswith("win"):
ports = ("COM{}".format(idx + 1) for idx in range(256))
if value in ports:
return value
raise vol.Invalid('{} is not a serial port'.format(value))
raise vol.Invalid("{} is not a serial port".format(value))
return cv.isdevice(value)
@ -44,7 +54,7 @@ def is_socket_address(value):
socket.getaddrinfo(value, None)
return value
except OSError:
raise vol.Invalid('Device is not a valid domain name or ip address')
raise vol.Invalid("Device is not a valid domain name or ip address")
def get_mysensors_gateway(hass, gateway_id):
@ -63,9 +73,9 @@ async def setup_gateways(hass, config):
for index, gateway_conf in enumerate(conf[CONF_GATEWAYS]):
persistence_file = gateway_conf.get(
CONF_PERSISTENCE_FILE,
hass.config.path('mysensors{}.pickle'.format(index + 1)))
ready_gateway = await _get_gateway(
hass, config, gateway_conf, persistence_file)
hass.config.path("mysensors{}.pickle".format(index + 1)),
)
ready_gateway = await _get_gateway(hass, config, gateway_conf, persistence_file)
if ready_gateway is not None:
gateways[id(ready_gateway)] = ready_gateway
@ -82,8 +92,8 @@ async def _get_gateway(hass, config, gateway_conf, persistence_file):
device = gateway_conf[CONF_DEVICE]
baud_rate = gateway_conf[CONF_BAUD_RATE]
tcp_port = gateway_conf[CONF_TCP_PORT]
in_prefix = gateway_conf.get(CONF_TOPIC_IN_PREFIX, '')
out_prefix = gateway_conf.get(CONF_TOPIC_OUT_PREFIX, '')
in_prefix = gateway_conf.get(CONF_TOPIC_IN_PREFIX, "")
out_prefix = gateway_conf.get(CONF_TOPIC_OUT_PREFIX, "")
if device == MQTT_COMPONENT:
if not await async_setup_component(hass, MQTT_COMPONENT, config):
@ -97,36 +107,51 @@ async def _get_gateway(hass, config, gateway_conf, persistence_file):
def sub_callback(topic, sub_cb, qos):
"""Call MQTT subscribe function."""
@callback
def internal_callback(msg):
"""Call callback."""
sub_cb(msg.topic, msg.payload, msg.qos)
hass.async_create_task(
mqtt.async_subscribe(topic, internal_callback, qos))
hass.async_create_task(mqtt.async_subscribe(topic, internal_callback, qos))
gateway = mysensors.AsyncMQTTGateway(
pub_callback, sub_callback, in_prefix=in_prefix,
out_prefix=out_prefix, retain=retain, loop=hass.loop,
event_callback=None, persistence=persistence,
pub_callback,
sub_callback,
in_prefix=in_prefix,
out_prefix=out_prefix,
retain=retain,
loop=hass.loop,
event_callback=None,
persistence=persistence,
persistence_file=persistence_file,
protocol_version=version)
protocol_version=version,
)
else:
try:
await hass.async_add_job(is_serial_port, device)
gateway = mysensors.AsyncSerialGateway(
device, baud=baud_rate, loop=hass.loop,
event_callback=None, persistence=persistence,
device,
baud=baud_rate,
loop=hass.loop,
event_callback=None,
persistence=persistence,
persistence_file=persistence_file,
protocol_version=version)
protocol_version=version,
)
except vol.Invalid:
try:
await hass.async_add_job(is_socket_address, device)
# valid ip address
gateway = mysensors.AsyncTCPGateway(
device, port=tcp_port, loop=hass.loop, event_callback=None,
persistence=persistence, persistence_file=persistence_file,
protocol_version=version)
device,
port=tcp_port,
loop=hass.loop,
event_callback=None,
persistence=persistence,
persistence_file=persistence_file,
protocol_version=version,
)
except vol.Invalid:
# invalid ip address
return None
@ -146,8 +171,7 @@ async def finish_setup(hass, hass_config, gateways):
discover_tasks = []
start_tasks = []
for gateway in gateways.values():
discover_tasks.append(_discover_persistent_devices(
hass, hass_config, gateway))
discover_tasks.append(_discover_persistent_devices(hass, hass_config, gateway))
start_tasks.append(_gw_start(hass, gateway))
if discover_tasks:
# Make sure all devices and platforms are loaded before gateway start.
@ -169,8 +193,7 @@ async def _discover_persistent_devices(hass, hass_config, gateway):
for platform, dev_ids in validated.items():
new_devices[platform].extend(dev_ids)
for platform, dev_ids in new_devices.items():
tasks.append(discover_mysensors_platform(
hass, hass_config, platform, dev_ids))
tasks.append(discover_mysensors_platform(hass, hass_config, platform, dev_ids))
if tasks:
await asyncio.wait(tasks)
@ -188,7 +211,7 @@ async def _gw_start(hass, gateway):
connect_task.cancel()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, gw_stop)
if gateway.device == 'mqtt':
if gateway.device == "mqtt":
# Gatways connected via mqtt doesn't send gateway ready message.
return
gateway_ready = asyncio.Future()
@ -201,18 +224,20 @@ async def _gw_start(hass, gateway):
except asyncio.TimeoutError:
_LOGGER.warning(
"Gateway %s not ready after %s secs so continuing with setup",
gateway.device, GATEWAY_READY_TIMEOUT)
gateway.device,
GATEWAY_READY_TIMEOUT,
)
finally:
hass.data.pop(gateway_ready_key, None)
def _gw_callback_factory(hass, hass_config):
"""Return a new callback for the gateway."""
@callback
def mysensors_callback(msg):
"""Handle messages from a MySensors gateway."""
_LOGGER.debug(
"Node update: node %s child %s", msg.node_id, msg.child_id)
_LOGGER.debug("Node update: node %s child %s", msg.node_id, msg.child_id)
msg_type = msg.gateway.const.MessageType(msg.type)
msg_handler = HANDLERS.get(msg_type.name)