Move imports to top for mochad (#29514)

* Move imports to top for mochad

* Fix test test_light.py for mochad

* Fix test test_switch.py for mochad

* Make intra package imports relative in switch and light
This commit is contained in:
springstan 2019-12-06 02:02:34 +01:00 committed by Martin Hjelmare
parent 9ba9b3339b
commit 3b6bc9067f
5 changed files with 35 additions and 34 deletions

View file

@ -2,11 +2,16 @@
import logging
import threading
from pymochad import controller, exceptions
import voluptuous as vol
from homeassistant.const import (
CONF_HOST,
CONF_PORT,
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP
from homeassistant.const import CONF_HOST, CONF_PORT
_LOGGER = logging.getLogger(__name__)
@ -37,8 +42,6 @@ def setup(hass, config):
host = conf.get(CONF_HOST)
port = conf.get(CONF_PORT)
from pymochad import exceptions
global CONTROLLER
try:
CONTROLLER = MochadCtrl(host, port)
@ -68,8 +71,6 @@ class MochadCtrl:
self._host = host
self._port = port
from pymochad import controller
self.ctrl = controller.PyMochad(server=self._host, port=self._port)
@property

View file

@ -1,30 +1,32 @@
"""Support for X10 dimmer over Mochad."""
import logging
from pymochad import device
import voluptuous as vol
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
PLATFORM_SCHEMA,
SUPPORT_BRIGHTNESS,
Light,
PLATFORM_SCHEMA,
)
from homeassistant.components import mochad
from homeassistant.const import CONF_NAME, CONF_PLATFORM, CONF_DEVICES, CONF_ADDRESS
from homeassistant.const import CONF_ADDRESS, CONF_DEVICES, CONF_NAME, CONF_PLATFORM
from homeassistant.helpers import config_validation as cv
from . import CONF_COMM_TYPE, CONTROLLER, DOMAIN, REQ_LOCK
_LOGGER = logging.getLogger(__name__)
CONF_BRIGHTNESS_LEVELS = "brightness_levels"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_PLATFORM): mochad.DOMAIN,
vol.Required(CONF_PLATFORM): DOMAIN,
CONF_DEVICES: [
{
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_ADDRESS): cv.x10_address,
vol.Optional(mochad.CONF_COMM_TYPE): cv.string,
vol.Optional(CONF_COMM_TYPE): cv.string,
vol.Optional(CONF_BRIGHTNESS_LEVELS, default=32): vol.All(
vol.Coerce(int), vol.In([32, 64, 256])
),
@ -37,7 +39,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up X10 dimmers over a mochad controller."""
devs = config.get(CONF_DEVICES)
add_entities([MochadLight(hass, mochad.CONTROLLER.ctrl, dev) for dev in devs])
add_entities([MochadLight(hass, CONTROLLER.ctrl, dev) for dev in devs])
return True
@ -46,12 +48,11 @@ class MochadLight(Light):
def __init__(self, hass, ctrl, dev):
"""Initialize a Mochad Light Device."""
from pymochad import device
self._controller = ctrl
self._address = dev[CONF_ADDRESS]
self._name = dev.get(CONF_NAME, f"x10_light_dev_{self._address}")
self._comm_type = dev.get(mochad.CONF_COMM_TYPE, "pl")
self._comm_type = dev.get(CONF_COMM_TYPE, "pl")
self.light = device.Device(ctrl, self._address, comm_type=self._comm_type)
self._brightness = 0
self._state = self._get_device_status()
@ -64,7 +65,7 @@ class MochadLight(Light):
def _get_device_status(self):
"""Get the status of the light from mochad."""
with mochad.REQ_LOCK:
with REQ_LOCK:
status = self.light.get_status().rstrip()
return status == "on"
@ -106,7 +107,7 @@ class MochadLight(Light):
def turn_on(self, **kwargs):
"""Send the command to turn the light on."""
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
with mochad.REQ_LOCK:
with REQ_LOCK:
if self._brightness_levels > 32:
out_brightness = self._calculate_brightness_value(brightness)
self.light.send_cmd(f"xdim {out_brightness}")
@ -124,7 +125,7 @@ class MochadLight(Light):
def turn_off(self, **kwargs):
"""Send the command to turn the light on."""
with mochad.REQ_LOCK:
with REQ_LOCK:
self.light.send_cmd("off")
self._controller.read_data()
# There is no persistence for X10 modules so we need to prepare

View file

@ -1,24 +1,27 @@
"""Support for X10 switch over Mochad."""
import logging
from pymochad import device
from pymochad.exceptions import MochadException
import voluptuous as vol
from homeassistant.components import mochad
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import CONF_NAME, CONF_DEVICES, CONF_PLATFORM, CONF_ADDRESS
from homeassistant.const import CONF_ADDRESS, CONF_DEVICES, CONF_NAME, CONF_PLATFORM
from homeassistant.helpers import config_validation as cv
from . import CONF_COMM_TYPE, CONTROLLER, DOMAIN, REQ_LOCK
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = vol.Schema(
{
vol.Required(CONF_PLATFORM): mochad.DOMAIN,
vol.Required(CONF_PLATFORM): DOMAIN,
CONF_DEVICES: [
{
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_ADDRESS): cv.x10_address,
vol.Optional(mochad.CONF_COMM_TYPE): cv.string,
vol.Optional(CONF_COMM_TYPE): cv.string,
}
],
}
@ -28,7 +31,7 @@ PLATFORM_SCHEMA = vol.Schema(
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up X10 switches over a mochad controller."""
devs = config.get(CONF_DEVICES)
add_entities([MochadSwitch(hass, mochad.CONTROLLER.ctrl, dev) for dev in devs])
add_entities([MochadSwitch(hass, CONTROLLER.ctrl, dev) for dev in devs])
return True
@ -37,12 +40,11 @@ class MochadSwitch(SwitchDevice):
def __init__(self, hass, ctrl, dev):
"""Initialize a Mochad Switch Device."""
from pymochad import device
self._controller = ctrl
self._address = dev[CONF_ADDRESS]
self._name = dev.get(CONF_NAME, "x10_switch_dev_%s" % self._address)
self._comm_type = dev.get(mochad.CONF_COMM_TYPE, "pl")
self._comm_type = dev.get(CONF_COMM_TYPE, "pl")
self.switch = device.Device(ctrl, self._address, comm_type=self._comm_type)
# Init with false to avoid locking HA for long on CM19A (goes from rf
# to pl via TM751, but not other way around)
@ -58,10 +60,9 @@ class MochadSwitch(SwitchDevice):
def turn_on(self, **kwargs):
"""Turn the switch on."""
from pymochad.exceptions import MochadException
_LOGGER.debug("Reconnect %s:%s", self._controller.server, self._controller.port)
with mochad.REQ_LOCK:
with REQ_LOCK:
try:
# Recycle socket on new command to recover mochad connection
self._controller.reconnect()
@ -75,10 +76,9 @@ class MochadSwitch(SwitchDevice):
def turn_off(self, **kwargs):
"""Turn the switch off."""
from pymochad.exceptions import MochadException
_LOGGER.debug("Reconnect %s:%s", self._controller.server, self._controller.port)
with mochad.REQ_LOCK:
with REQ_LOCK:
try:
# Recycle socket on new command to recover mochad connection
self._controller.reconnect()
@ -92,7 +92,7 @@ class MochadSwitch(SwitchDevice):
def _get_device_status(self):
"""Get the status of the switch from mochad."""
with mochad.REQ_LOCK:
with REQ_LOCK:
status = self.switch.get_status().rstrip()
return status == "on"

View file

@ -14,8 +14,8 @@ from tests.common import get_test_home_assistant
@pytest.fixture(autouse=True)
def pymochad_mock():
"""Mock pymochad."""
with mock.patch.dict("sys.modules", {"pymochad": mock.MagicMock()}):
yield
with mock.patch("homeassistant.components.mochad.light.device") as device:
yield device
class TestMochadSwitchSetup(unittest.TestCase):

View file

@ -14,9 +14,8 @@ from tests.common import get_test_home_assistant
@pytest.fixture(autouse=True)
def pymochad_mock():
"""Mock pymochad."""
with mock.patch.dict(
"sys.modules",
{"pymochad": mock.MagicMock(), "pymochad.exceptions": mock.MagicMock()},
with mock.patch("homeassistant.components.mochad.switch.device"), mock.patch(
"homeassistant.components.mochad.switch.MochadException"
):
yield