Less fhem output

This commit is contained in:
Martin Bauer 2020-04-19 15:59:51 +02:00
parent 685a2199fc
commit 98aa37083b
4 changed files with 55 additions and 3 deletions

View File

@ -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]

View File

@ -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

View File

@ -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}")

View File

@ -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())