core/homeassistant/components/ebusd/__init__.py

123 lines
3.7 KiB
Python
Raw Normal View History

"""Support for Ebusd daemon for communication with eBUS heating systems."""
import logging
import socket
import ebusdpy
import voluptuous as vol
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
CONF_HOST,
CONF_MONITORED_CONDITIONS,
CONF_NAME,
CONF_PORT,
2019-07-31 19:25:30 +00:00
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import load_platform
2019-07-31 19:25:30 +00:00
from .const import DOMAIN, SENSOR_TYPES
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "ebusd"
DEFAULT_PORT = 8888
2019-07-31 19:25:30 +00:00
CONF_CIRCUIT = "circuit"
CACHE_TTL = 900
2019-07-31 19:25:30 +00:00
SERVICE_EBUSD_WRITE = "ebusd_write"
def verify_ebusd_config(config):
"""Verify eBusd config."""
circuit = config[CONF_CIRCUIT]
for condition in config[CONF_MONITORED_CONDITIONS]:
if condition not in SENSOR_TYPES[circuit]:
raise vol.Invalid(f"Condition '{condition}' not in '{circuit}'.")
return config
2019-07-31 19:25:30 +00:00
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
vol.All(
{
vol.Required(CONF_CIRCUIT): cv.string,
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): cv.ensure_list,
},
verify_ebusd_config,
)
)
},
extra=vol.ALLOW_EXTRA,
)
def setup(hass, config):
"""Set up the eBusd component."""
_LOGGER.debug("Integration setup started")
conf = config[DOMAIN]
name = conf[CONF_NAME]
circuit = conf[CONF_CIRCUIT]
monitored_conditions = conf.get(CONF_MONITORED_CONDITIONS)
2019-07-31 19:25:30 +00:00
server_address = (conf.get(CONF_HOST), conf.get(CONF_PORT))
try:
2019-07-31 19:25:30 +00:00
ebusdpy.init(server_address)
hass.data[DOMAIN] = EbusdData(server_address, circuit)
sensor_config = {
CONF_MONITORED_CONDITIONS: monitored_conditions,
2019-07-31 19:25:30 +00:00
"client_name": name,
"sensor_types": SENSOR_TYPES[circuit],
}
2019-07-31 19:25:30 +00:00
load_platform(hass, "sensor", DOMAIN, sensor_config, config)
2019-07-31 19:25:30 +00:00
hass.services.register(DOMAIN, SERVICE_EBUSD_WRITE, hass.data[DOMAIN].write)
_LOGGER.debug("Ebusd integration setup completed")
return True
except (socket.timeout, OSError):
return False
class EbusdData:
"""Get the latest data from Ebusd."""
def __init__(self, address, circuit):
"""Initialize the data object."""
self._circuit = circuit
self._address = address
self.value = {}
def update(self, name, stype):
"""Call the Ebusd API to update the data."""
try:
_LOGGER.debug("Opening socket to ebusd %s", name)
command_result = ebusdpy.read(
2019-07-31 19:25:30 +00:00
self._address, self._circuit, name, stype, CACHE_TTL
)
if command_result is not None:
2019-07-31 19:25:30 +00:00
if "ERR:" in command_result:
_LOGGER.warning(command_result)
else:
self.value[name] = command_result
except RuntimeError as err:
_LOGGER.error(err)
raise RuntimeError(err) from err
def write(self, call):
"""Call write methon on ebusd."""
2019-07-31 19:25:30 +00:00
name = call.data.get("name")
value = call.data.get("value")
try:
_LOGGER.debug("Opening socket to ebusd %s", name)
2019-07-31 19:25:30 +00:00
command_result = ebusdpy.write(self._address, self._circuit, name, value)
if command_result is not None:
2019-07-31 19:25:30 +00:00
if "done" not in command_result:
_LOGGER.warning("Write command failed: %s", name)
except RuntimeError as err:
_LOGGER.error(err)