Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support to control a Zehnder ComfoAir Q350/450/600 ventilation unit."""
2 
3 import logging
4 
5 from pycomfoconnect import Bridge, ComfoConnect
6 import voluptuous as vol
7 
8 from homeassistant.const import (
9  CONF_HOST,
10  CONF_NAME,
11  CONF_PIN,
12  CONF_TOKEN,
13  EVENT_HOMEASSISTANT_STOP,
14  Platform,
15 )
16 from homeassistant.core import Event, HomeAssistant
17 from homeassistant.helpers import discovery
19 from homeassistant.helpers.dispatcher import dispatcher_send
20 from homeassistant.helpers.typing import ConfigType
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 DOMAIN = "comfoconnect"
25 
26 SIGNAL_COMFOCONNECT_UPDATE_RECEIVED = "comfoconnect_update_received_{}"
27 
28 CONF_USER_AGENT = "user_agent"
29 
30 DEFAULT_NAME = "ComfoAirQ"
31 DEFAULT_PIN = 0
32 DEFAULT_TOKEN = "00000000000000000000000000000001"
33 DEFAULT_USER_AGENT = "Home Assistant"
34 
35 CONFIG_SCHEMA = vol.Schema(
36  {
37  DOMAIN: vol.Schema(
38  {
39  vol.Required(CONF_HOST): cv.string,
40  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
41  vol.Optional(CONF_TOKEN, default=DEFAULT_TOKEN): vol.Length(
42  min=32, max=32, msg="invalid token"
43  ),
44  vol.Optional(CONF_USER_AGENT, default=DEFAULT_USER_AGENT): cv.string,
45  vol.Optional(CONF_PIN, default=DEFAULT_PIN): cv.positive_int,
46  }
47  )
48  },
49  extra=vol.ALLOW_EXTRA,
50 )
51 
52 
53 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
54  """Set up the ComfoConnect bridge."""
55 
56  conf = config[DOMAIN]
57  host = conf[CONF_HOST]
58  name = conf[CONF_NAME]
59  token = conf[CONF_TOKEN]
60  user_agent = conf[CONF_USER_AGENT]
61  pin = conf[CONF_PIN]
62 
63  # Run discovery on the configured ip
64  bridges = Bridge.discover(host)
65  if not bridges:
66  _LOGGER.error("Could not connect to ComfoConnect bridge on %s", host)
67  return False
68  bridge = bridges[0]
69  _LOGGER.debug("Bridge found: %s (%s)", bridge.uuid.hex(), bridge.host)
70 
71  # Setup ComfoConnect Bridge
72  ccb = ComfoConnectBridge(hass, bridge, name, token, user_agent, pin)
73  hass.data[DOMAIN] = ccb
74 
75  # Start connection with bridge
76  ccb.connect()
77 
78  # Schedule disconnect on shutdown
79  def _shutdown(_event: Event) -> None:
80  ccb.disconnect()
81 
82  hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown)
83 
84  # Load platforms
85  discovery.load_platform(hass, Platform.FAN, DOMAIN, {}, config)
86 
87  return True
88 
89 
91  """Representation of a ComfoConnect bridge."""
92 
93  def __init__(
94  self,
95  hass: HomeAssistant,
96  bridge: Bridge,
97  name: str,
98  token: str,
99  friendly_name: str,
100  pin: int,
101  ) -> None:
102  """Initialize the ComfoConnect bridge."""
103  self.namename = name
104  self.hasshass = hass
105  self.unique_idunique_id = bridge.uuid.hex()
106 
107  self.comfoconnectcomfoconnect = ComfoConnect(
108  bridge=bridge,
109  local_uuid=bytes.fromhex(token),
110  local_devicename=friendly_name,
111  pin=pin,
112  )
113  self.comfoconnectcomfoconnect.callback_sensor = self.sensor_callbacksensor_callback
114 
115  def connect(self) -> None:
116  """Connect with the bridge."""
117  _LOGGER.debug("Connecting with bridge")
118  self.comfoconnectcomfoconnect.connect(True)
119 
120  def disconnect(self) -> None:
121  """Disconnect from the bridge."""
122  _LOGGER.debug("Disconnecting from bridge")
123  self.comfoconnectcomfoconnect.disconnect()
124 
125  def sensor_callback(self, var: str, value: str) -> None:
126  """Notify listeners that we have received an update."""
127  _LOGGER.debug("Received update for %s: %s", var, value)
129  self.hasshass, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED.format(var), value
130  )
None sensor_callback(self, str var, str value)
Definition: __init__.py:125
None __init__(self, HomeAssistant hass, Bridge bridge, str name, str token, str friendly_name, int pin)
Definition: __init__.py:101
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:53
None dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:137