Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Ebusd daemon for communication with eBUS heating systems."""
2 
3 import logging
4 
5 import ebusdpy
6 import voluptuous as vol
7 
8 from homeassistant.const import (
9  CONF_HOST,
10  CONF_MONITORED_CONDITIONS,
11  CONF_NAME,
12  CONF_PORT,
13  Platform,
14 )
15 from homeassistant.core import HomeAssistant, ServiceCall
17 from homeassistant.helpers.discovery import load_platform
18 from homeassistant.helpers.typing import ConfigType
19 
20 from .const import DOMAIN, SENSOR_TYPES
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 DEFAULT_NAME = "ebusd"
25 DEFAULT_PORT = 8888
26 CONF_CIRCUIT = "circuit"
27 CACHE_TTL = 900
28 SERVICE_EBUSD_WRITE = "ebusd_write"
29 
30 
31 def verify_ebusd_config(config):
32  """Verify eBusd config."""
33  circuit = config[CONF_CIRCUIT]
34  for condition in config[CONF_MONITORED_CONDITIONS]:
35  if condition not in SENSOR_TYPES[circuit]:
36  raise vol.Invalid(f"Condition '{condition}' not in '{circuit}'.")
37  return config
38 
39 
40 CONFIG_SCHEMA = vol.Schema(
41  {
42  DOMAIN: vol.Schema(
43  vol.All(
44  {
45  vol.Required(CONF_CIRCUIT): cv.string,
46  vol.Required(CONF_HOST): cv.string,
47  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
48  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
49  vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): cv.ensure_list,
50  },
51  verify_ebusd_config,
52  )
53  )
54  },
55  extra=vol.ALLOW_EXTRA,
56 )
57 
58 
59 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
60  """Set up the eBusd component."""
61  _LOGGER.debug("Integration setup started")
62  conf = config[DOMAIN]
63  name = conf[CONF_NAME]
64  circuit = conf[CONF_CIRCUIT]
65  monitored_conditions = conf.get(CONF_MONITORED_CONDITIONS)
66  server_address = (conf.get(CONF_HOST), conf.get(CONF_PORT))
67 
68  try:
69  ebusdpy.init(server_address)
70  except (TimeoutError, OSError):
71  return False
72  hass.data[DOMAIN] = EbusdData(server_address, circuit)
73  sensor_config = {
74  CONF_MONITORED_CONDITIONS: monitored_conditions,
75  "client_name": name,
76  "sensor_types": SENSOR_TYPES[circuit],
77  }
78  load_platform(hass, Platform.SENSOR, DOMAIN, sensor_config, config)
79 
80  hass.services.register(DOMAIN, SERVICE_EBUSD_WRITE, hass.data[DOMAIN].write)
81 
82  _LOGGER.debug("Ebusd integration setup completed")
83  return True
84 
85 
86 class EbusdData:
87  """Get the latest data from Ebusd."""
88 
89  def __init__(self, address, circuit):
90  """Initialize the data object."""
91  self._circuit_circuit = circuit
92  self._address_address = address
93  self.valuevalue = {}
94 
95  def update(self, name, stype):
96  """Call the Ebusd API to update the data."""
97  try:
98  _LOGGER.debug("Opening socket to ebusd %s", name)
99  command_result = ebusdpy.read(
100  self._address_address, self._circuit_circuit, name, stype, CACHE_TTL
101  )
102  if command_result is not None:
103  if "ERR:" in command_result:
104  _LOGGER.warning(command_result)
105  else:
106  self.valuevalue[name] = command_result
107  except RuntimeError as err:
108  _LOGGER.error(err)
109  raise RuntimeError(err) from err
110 
111  def write(self, call: ServiceCall) -> None:
112  """Call write method on ebusd."""
113  name = call.data.get("name")
114  value = call.data.get("value")
115 
116  try:
117  _LOGGER.debug("Opening socket to ebusd %s", name)
118  command_result = ebusdpy.write(self._address_address, self._circuit_circuit, name, value)
119  if command_result is not None and "done" not in command_result:
120  _LOGGER.warning("Write command failed: %s", name)
121  except RuntimeError as err:
122  _LOGGER.error(err)
def update(self, name, stype)
Definition: __init__.py:95
def __init__(self, address, circuit)
Definition: __init__.py:89
None write(self, ServiceCall call)
Definition: __init__.py:111
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:59
def verify_ebusd_config(config)
Definition: __init__.py:31
None load_platform(core.HomeAssistant hass, Platform|str component, str platform, DiscoveryInfoType|None discovered, ConfigType hass_config)
Definition: discovery.py:137