Reconnect mochad light on on/off command (#44507)

This commit is contained in:
migube 2021-01-03 20:01:05 +01:00 committed by GitHub
parent 1fc4284a29
commit 10b5912901
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,8 @@
"""Support for X10 dimmer over Mochad.""" """Support for X10 dimmer over Mochad."""
import logging
from pymochad import device from pymochad import device
from pymochad.exceptions import MochadException
import voluptuous as vol import voluptuous as vol
from homeassistant.components.light import ( from homeassistant.components.light import (
@ -13,6 +16,7 @@ from homeassistant.helpers import config_validation as cv
from . import CONF_COMM_TYPE, DOMAIN, REQ_LOCK from . import CONF_COMM_TYPE, DOMAIN, REQ_LOCK
_LOGGER = logging.getLogger(__name__)
CONF_BRIGHTNESS_LEVELS = "brightness_levels" CONF_BRIGHTNESS_LEVELS = "brightness_levels"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -103,30 +107,42 @@ class MochadLight(LightEntity):
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Send the command to turn the light on.""" """Send the command to turn the light on."""
_LOGGER.debug("Reconnect %s:%s", self._controller.server, self._controller.port)
brightness = kwargs.get(ATTR_BRIGHTNESS, 255) brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
with REQ_LOCK: with REQ_LOCK:
if self._brightness_levels > 32: try:
out_brightness = self._calculate_brightness_value(brightness) # Recycle socket on new command to recover mochad connection
self.light.send_cmd(f"xdim {out_brightness}") self._controller.reconnect()
self._controller.read_data() if self._brightness_levels > 32:
else: out_brightness = self._calculate_brightness_value(brightness)
self.light.send_cmd("on") self.light.send_cmd(f"xdim {out_brightness}")
self._controller.read_data() self._controller.read_data()
# There is no persistence for X10 modules so a fresh on command else:
# will be full brightness self.light.send_cmd("on")
if self._brightness == 0: self._controller.read_data()
self._brightness = 255 # There is no persistence for X10 modules so a fresh on command
self._adjust_brightness(brightness) # will be full brightness
self._brightness = brightness if self._brightness == 0:
self._state = True self._brightness = 255
self._adjust_brightness(brightness)
self._brightness = brightness
self._state = True
except (MochadException, OSError) as exc:
_LOGGER.error("Error with mochad communication: %s", exc)
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Send the command to turn the light on.""" """Send the command to turn the light on."""
_LOGGER.debug("Reconnect %s:%s", self._controller.server, self._controller.port)
with REQ_LOCK: with REQ_LOCK:
self.light.send_cmd("off") try:
self._controller.read_data() # Recycle socket on new command to recover mochad connection
# There is no persistence for X10 modules so we need to prepare self._controller.reconnect()
# to track a fresh on command will full brightness self.light.send_cmd("off")
if self._brightness_levels == 31: self._controller.read_data()
self._brightness = 0 # There is no persistence for X10 modules so we need to prepare
self._state = False # to track a fresh on command will full brightness
if self._brightness_levels == 31:
self._brightness = 0
self._state = False
except (MochadException, OSError) as exc:
_LOGGER.error("Error with mochad communication: %s", exc)