core/homeassistant/components/comfoconnect/__init__.py

138 lines
3.9 KiB
Python
Raw Normal View History

"""Support to control a Zehnder ComfoAir Q350/450/600 ventilation unit."""
import logging
from pycomfoconnect import (
SENSOR_TEMPERATURE_EXTRACT,
SENSOR_TEMPERATURE_OUTDOOR,
Bridge,
ComfoConnect,
)
import voluptuous as vol
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
CONF_HOST,
CONF_NAME,
CONF_PIN,
CONF_TOKEN,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import dispatcher_send
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
DOMAIN = "comfoconnect"
2019-07-31 19:25:30 +00:00
SIGNAL_COMFOCONNECT_UPDATE_RECEIVED = "comfoconnect_update_received"
2019-07-31 19:25:30 +00:00
ATTR_CURRENT_TEMPERATURE = "current_temperature"
ATTR_CURRENT_HUMIDITY = "current_humidity"
ATTR_OUTSIDE_TEMPERATURE = "outside_temperature"
ATTR_OUTSIDE_HUMIDITY = "outside_humidity"
ATTR_AIR_FLOW_SUPPLY = "air_flow_supply"
ATTR_AIR_FLOW_EXHAUST = "air_flow_exhaust"
2019-07-31 19:25:30 +00:00
CONF_USER_AGENT = "user_agent"
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "ComfoAirQ"
DEFAULT_PIN = 0
2019-07-31 19:25:30 +00:00
DEFAULT_TOKEN = "00000000000000000000000000000001"
DEFAULT_USER_AGENT = "Home Assistant"
2017-06-29 09:44:35 +00:00
DEVICE = None
2019-07-31 19:25:30 +00:00
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_TOKEN, default=DEFAULT_TOKEN): vol.Length(
min=32, max=32, msg="invalid token"
),
vol.Optional(CONF_USER_AGENT, default=DEFAULT_USER_AGENT): cv.string,
vol.Optional(CONF_PIN, default=DEFAULT_PIN): cv.positive_int,
}
)
},
extra=vol.ALLOW_EXTRA,
)
def setup(hass, config):
2017-06-29 09:44:35 +00:00
"""Set up the ComfoConnect bridge."""
conf = config[DOMAIN]
host = conf.get(CONF_HOST)
name = conf.get(CONF_NAME)
token = conf.get(CONF_TOKEN)
user_agent = conf.get(CONF_USER_AGENT)
pin = conf.get(CONF_PIN)
# Run discovery on the configured ip
bridges = Bridge.discover(host)
if not bridges:
2017-06-29 09:44:35 +00:00
_LOGGER.error("Could not connect to ComfoConnect bridge on %s", host)
return False
bridge = bridges[0]
2017-06-29 09:44:35 +00:00
_LOGGER.info("Bridge found: %s (%s)", bridge.uuid.hex(), bridge.host)
# Setup ComfoConnect Bridge
ccb = ComfoConnectBridge(hass, bridge, name, token, user_agent, pin)
hass.data[DOMAIN] = ccb
# Start connection with bridge
ccb.connect()
# Schedule disconnect on shutdown
def _shutdown(_event):
ccb.disconnect()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown)
# Load platforms
2019-07-31 19:25:30 +00:00
discovery.load_platform(hass, "fan", DOMAIN, {}, config)
return True
class ComfoConnectBridge:
"""Representation of a ComfoConnect bridge."""
def __init__(self, hass, bridge, name, token, friendly_name, pin):
"""Initialize the ComfoConnect bridge."""
self.data = {}
self.name = name
self.hass = hass
2017-06-29 09:44:35 +00:00
self.comfoconnect = ComfoConnect(
2019-07-31 19:25:30 +00:00
bridge=bridge,
local_uuid=bytes.fromhex(token),
local_devicename=friendly_name,
pin=pin,
)
self.comfoconnect.callback_sensor = self.sensor_callback
def connect(self):
"""Connect with the bridge."""
2017-06-29 09:44:35 +00:00
_LOGGER.debug("Connecting with bridge")
self.comfoconnect.connect(True)
def disconnect(self):
"""Disconnect from the bridge."""
2017-06-29 09:44:35 +00:00
_LOGGER.debug("Disconnecting from bridge")
self.comfoconnect.disconnect()
def sensor_callback(self, var, value):
"""Call function for sensor updates."""
2017-06-29 09:44:35 +00:00
_LOGGER.debug("Got value from bridge: %d = %d", var, value)
if var in [SENSOR_TEMPERATURE_EXTRACT, SENSOR_TEMPERATURE_OUTDOOR]:
self.data[var] = value / 10
else:
self.data[var] = value
# Notify listeners that we have received an update
dispatcher_send(self.hass, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, var)