From 354fffa5ee5bebebeeb282da5c5d887e190429db Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Sun, 19 Apr 2020 16:00:50 +0200 Subject: [PATCH] Lirc: sensor showing connected hosts --- custom_components/lirc_network/__init__.py | 4 +++- custom_components/lirc_network/binary_sensor.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 custom_components/lirc_network/binary_sensor.py diff --git a/custom_components/lirc_network/__init__.py b/custom_components/lirc_network/__init__.py index f474379..ca75216 100644 --- a/custom_components/lirc_network/__init__.py +++ b/custom_components/lirc_network/__init__.py @@ -14,6 +14,7 @@ DOMAIN = 'lirc_network' REMOTE_NAME = 'remote' REPEAT_COUNTER = 'repeat_counter' LIRC_HOST = 'host' +DATA_LIRC_NETWORK = 'data_lirc_network' CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema(vol.All([{ @@ -24,9 +25,10 @@ CONFIG_SCHEMA = vol.Schema({ async def async_setup(hass, config): + hass.data[DATA_LIRC_NETWORK] = [] for config_element in config[DOMAIN]: - _LOGGER.warning(f'Connecting to {config_element[CONF_HOST]} at {config_element[CONF_PORT]}') connection = LircConnection(hass, config_element) + hass.data[DATA_LIRC_NETWORK].append(connection) await connection.start() _LOGGER.warning("About to return true in async_setup") return True diff --git a/custom_components/lirc_network/binary_sensor.py b/custom_components/lirc_network/binary_sensor.py new file mode 100644 index 0000000..0845d8e --- /dev/null +++ b/custom_components/lirc_network/binary_sensor.py @@ -0,0 +1,15 @@ +import logging +from ..reconnecting_client import IsConnectedSensor +from . import DATA_LIRC_NETWORK + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): + connections = hass.data[DATA_LIRC_NETWORK] + sensors = [] + for connection in connections: + sensor = IsConnectedSensor(connection.get_name() + "_connected") + connection.connected_sensor = sensor + sensors.append(sensor) + async_add_entities(sensors)