From 98aa37083b1e9b1971708eab123e39d664a99e51 Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Sun, 19 Apr 2020 15:59:51 +0200 Subject: [PATCH] Less fhem output --- config_creation/main.py | 2 +- config_creation/manual_config.yaml | 2 + custom_components/fhem/__init__.py | 4 +- custom_components/reconnecting_client.py | 50 ++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/config_creation/main.py b/config_creation/main.py index f784f24..86e5da2 100644 --- a/config_creation/main.py +++ b/config_creation/main.py @@ -267,7 +267,7 @@ def create_config(target_directory, development=False): with open(os.path.join(target_directory, 'configuration.yaml'), 'w') as output: output.write("# Dont' edit manually! this is generated!\n\n") - for key in ['sensor', 'switch', 'light', 'cover']: + for key in ['sensor', 'switch', 'light', 'cover', 'binary_sensor']: if key in manual_config_dict: all_devices[key].extend(manual_config_dict[key]) del manual_config_dict[key] diff --git a/config_creation/manual_config.yaml b/config_creation/manual_config.yaml index d97281c..de6d561 100644 --- a/config_creation/manual_config.yaml +++ b/config_creation/manual_config.yaml @@ -149,6 +149,8 @@ lirc_network: - host: musikserverWohnzimmerOben port: 8765 +binary_sensor: + - platform: lirc_network # sensor to show for each host if it is currently connected media_player: - platform: squeezebox_telnet diff --git a/custom_components/fhem/__init__.py b/custom_components/fhem/__init__.py index 73b267d..0a9ebca 100644 --- a/custom_components/fhem/__init__.py +++ b/custom_components/fhem/__init__.py @@ -56,7 +56,7 @@ class FhemConnection(ReconnectingClient): if line.startswith(self._cul_device_name + " "): # Status update message _, device_name, command = line.split(" ", 2) for device in self._devices[device_name]: - _LOGGER.debug("FHEM line received (device): " + device_name + ": " + line) + #_LOGGER.debug("FHEM line received (device): " + device_name + ": " + line) await device.line_received(command.strip()) else: # potential response to displayattr split_line = line.split(" ", 1) @@ -69,7 +69,7 @@ class FhemConnection(ReconnectingClient): if self._writer: line += '\n' self._writer.write(line.encode()) - _LOGGER.debug(f"FHEM write line {line}") + #_LOGGER.debug(f"FHEM write line {line}") else: _LOGGER.debug(f"FHEM Failed to write line {line}") diff --git a/custom_components/reconnecting_client.py b/custom_components/reconnecting_client.py index 8ed0a2a..f5a1f3d 100644 --- a/custom_components/reconnecting_client.py +++ b/custom_components/reconnecting_client.py @@ -1,6 +1,34 @@ import asyncio import logging from homeassistant.const import EVENT_HOMEASSISTANT_STOP +from homeassistant.components.binary_sensor import BinarySensorDevice + + +class IsConnectedSensor(BinarySensorDevice): + + def __init__(self, name): + self._on = True + self._name = name + + async def set_value(self, value): + self._on = value + await self.async_update_ha_state() + + @property + def name(self): + return self._name + + @property + def should_poll(self): + return False + + @property + def available(self) -> bool: + return True + + @property + def is_on(self): + return self._on _LOGGER = logging.getLogger(__name__) @@ -23,6 +51,16 @@ class ReconnectingClient: self._connection_last_state = 'UNKNOWN' self._connection_name = connection_name self._connection_task = None + self._connected_sensor = None + + @property + def connected_sensor(self): + return self._connected_sensor + + @connected_sensor.setter + def connected_sensor(self, new_value): + self._connected_sensor = new_value + self._connected_sensor._on = self.connected async def start(self): self._run = True @@ -34,6 +72,9 @@ class ReconnectingClient: self._run = False self.connected = False + def get_name(self): + return "{}_{}".format(self._connection_name, self._host) + def write_line(self, line): try: if self._writer: @@ -54,6 +95,8 @@ class ReconnectingClient: self._writer = writer self.connected = True await self._connection_status_changed_callback('connected') + if self._connected_sensor: + await self._connected_sensor.set_value(True) self.reconnect_time = self.reconnect_time_start while self._run: @@ -70,11 +113,18 @@ class ReconnectingClient: self.hass.components.persistent_notification.async_create(notification_text, title="No connection") _LOGGER.error("Connection to {} failed {}:{}".format(self._connection_name, self._host, self._port)) await self._connection_status_changed_callback('disconnected') + if self._connected_sensor: + await self._connected_sensor.set_value(False) + _LOGGER.error( + "After connection failed ({} {}:{})".format(self._connection_name, self._host, self._port)) else: _LOGGER.debug(f"{self._connection_name} retried connection, last state {self._connection_last_state}" f"reconnection time {self.reconnect_time}") self._connection_last_state = 'FAILED' self.connected = False + _LOGGER.error("{} {} Sleeping for {}".format(self._connection_name, self._host, self.reconnect_time)) await asyncio.sleep(self.reconnect_time) self.reconnect_time = min(2 * self.reconnect_time, self.reconnect_time_max) + _LOGGER.error("{} {} creating connection task again {}".format(self._connection_name, self._host, + self.reconnect_time)) self._connection_task = self.hass.loop.create_task(self._connection())