FHEM & reconnecting client fixes
This commit is contained in:
@@ -41,9 +41,7 @@ class FhemConnection(ReconnectingClient):
|
||||
def register_device(self, id, d):
|
||||
self._devices[id].append(d)
|
||||
if self._writer:
|
||||
self._writer.writelines([
|
||||
"displayattr .*\n".encode(),
|
||||
])
|
||||
d.refresh()
|
||||
|
||||
async def _update_all_devices(self, state):
|
||||
if state == 'connected':
|
||||
@@ -51,6 +49,7 @@ class FhemConnection(ReconnectingClient):
|
||||
self.write_line("inform on")
|
||||
for device_list in self._devices.values():
|
||||
for device in device_list:
|
||||
device.refresh()
|
||||
await device.async_update_ha_state()
|
||||
|
||||
async def _process_line(self, line):
|
||||
|
||||
@@ -64,6 +64,9 @@ class FhemBinarySensor(BinarySensorDevice):
|
||||
def is_on(self):
|
||||
return self._state
|
||||
|
||||
def refresh(self):
|
||||
pass
|
||||
|
||||
async def line_received(self, line):
|
||||
if self._type == 'motion' and line.startswith('motion'):
|
||||
self._available = True
|
||||
|
||||
@@ -58,6 +58,9 @@ class FhemCover(CoverDevice):
|
||||
def current_cover_position(self):
|
||||
return self._position
|
||||
|
||||
def refresh(self):
|
||||
self.connection.fhem_set(self._ids[0], 'statusRequest')
|
||||
|
||||
@property
|
||||
def is_closed(self):
|
||||
"""Return if the cover is closed."""
|
||||
@@ -88,8 +91,7 @@ class FhemCover(CoverDevice):
|
||||
_, new_motor_state, new_position = line.split(':')
|
||||
new_position = new_position.strip().lower()
|
||||
new_motor_state = new_motor_state.strip().lower()
|
||||
assert new_motor_state == 'stop' or new_motor_state == 'up' or new_motor_state == 'down', \
|
||||
'Unknown motor state ' + new_motor_state
|
||||
assert new_motor_state in ('up', 'down', 'stop', 'err'), 'Unknown motor state ' + new_motor_state
|
||||
if new_motor_state == 'stop':
|
||||
if new_position == 'on':
|
||||
self._position = 100
|
||||
|
||||
@@ -72,6 +72,9 @@ class FhemLight(Light):
|
||||
else:
|
||||
return None
|
||||
|
||||
def refresh(self):
|
||||
self.connection.fhem_set(self._ids[0], 'statusRequest')
|
||||
|
||||
async def async_turn_on(self, **kwargs):
|
||||
brightness = kwargs.get(ATTR_BRIGHTNESS, None)
|
||||
transition_time = kwargs.get(ATTR_TRANSITION, 0.5)
|
||||
@@ -124,6 +127,7 @@ class FhemLight(Light):
|
||||
pass
|
||||
await self.async_update_ha_state()
|
||||
elif line.startswith('ResndFail') or line.startswith('MISSING ACK'):
|
||||
_LOGGER.warning(f"FHEM light {self.name} became unavailable: '{line}'")
|
||||
self._available = False
|
||||
await self.async_update_ha_state()
|
||||
else:
|
||||
|
||||
@@ -60,6 +60,9 @@ class FhemSensor(Entity):
|
||||
def state(self):
|
||||
return self._state
|
||||
|
||||
def refresh(self):
|
||||
pass
|
||||
|
||||
async def line_received(self, line):
|
||||
if self._type == 'brightness' and line.startswith('brightness'):
|
||||
self._available = True
|
||||
@@ -69,7 +72,7 @@ class FhemSensor(Entity):
|
||||
elif self._type == 'power' and line.startswith('power'):
|
||||
self._available = True
|
||||
_, new_value = line.split(':')
|
||||
self._state = int(new_value)
|
||||
self._state = float(new_value)
|
||||
await self.async_update_ha_state()
|
||||
elif line.startswith('ResndFail') or line.startswith('MISSING ACK'):
|
||||
self._available = False
|
||||
|
||||
@@ -53,24 +53,33 @@ class FhemSwitch(SwitchDevice):
|
||||
|
||||
async def async_turn_on(self, **kwargs):
|
||||
"""Turn the device on."""
|
||||
self._on = True
|
||||
self.connection.fhem_set(self._ids[0], 'on')
|
||||
|
||||
async def async_turn_off(self, **kwargs):
|
||||
"""Turn the device off."""
|
||||
self._on = False
|
||||
self.connection.fhem_set(self._ids[0], 'off')
|
||||
|
||||
def refresh(self):
|
||||
self.connection.fhem_set(self._ids[0], 'statusRequest')
|
||||
|
||||
async def line_received(self, line):
|
||||
line = line.strip()
|
||||
if line.startswith('level:'):
|
||||
self._available = True
|
||||
_, new_state = line.split(':')
|
||||
new_state = new_state.strip().lower()
|
||||
if new_state in ('on', '100'):
|
||||
self._on = True
|
||||
if new_state in ('off', '0'):
|
||||
elif new_state in ('off', '0'):
|
||||
self._on = False
|
||||
await self.async_update_ha_state()
|
||||
elif line in ('on', 'off'):
|
||||
self._available = True
|
||||
prev = self._on
|
||||
self._on = (line == 'on')
|
||||
await self.async_update_ha_state()
|
||||
elif line.startswith('ResndFail') or line.startswith('MISSING ACK'):
|
||||
self._available = False
|
||||
await self.async_update_ha_state()
|
||||
|
||||
Reference in New Issue
Block a user