mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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:
parent
9ba9b3339b
commit
3b6bc9067f
@ -2,11 +2,16 @@
|
|||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
from pymochad import controller, exceptions
|
||||||
import voluptuous as vol
|
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
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -37,8 +42,6 @@ def setup(hass, config):
|
|||||||
host = conf.get(CONF_HOST)
|
host = conf.get(CONF_HOST)
|
||||||
port = conf.get(CONF_PORT)
|
port = conf.get(CONF_PORT)
|
||||||
|
|
||||||
from pymochad import exceptions
|
|
||||||
|
|
||||||
global CONTROLLER
|
global CONTROLLER
|
||||||
try:
|
try:
|
||||||
CONTROLLER = MochadCtrl(host, port)
|
CONTROLLER = MochadCtrl(host, port)
|
||||||
@ -68,8 +71,6 @@ class MochadCtrl:
|
|||||||
self._host = host
|
self._host = host
|
||||||
self._port = port
|
self._port = port
|
||||||
|
|
||||||
from pymochad import controller
|
|
||||||
|
|
||||||
self.ctrl = controller.PyMochad(server=self._host, port=self._port)
|
self.ctrl = controller.PyMochad(server=self._host, port=self._port)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1,30 +1,32 @@
|
|||||||
"""Support for X10 dimmer over Mochad."""
|
"""Support for X10 dimmer over Mochad."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from pymochad import device
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
|
PLATFORM_SCHEMA,
|
||||||
SUPPORT_BRIGHTNESS,
|
SUPPORT_BRIGHTNESS,
|
||||||
Light,
|
Light,
|
||||||
PLATFORM_SCHEMA,
|
|
||||||
)
|
)
|
||||||
from homeassistant.components import mochad
|
from homeassistant.const import CONF_ADDRESS, CONF_DEVICES, CONF_NAME, CONF_PLATFORM
|
||||||
from homeassistant.const import CONF_NAME, CONF_PLATFORM, CONF_DEVICES, CONF_ADDRESS
|
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
|
|
||||||
|
from . import CONF_COMM_TYPE, CONTROLLER, DOMAIN, REQ_LOCK
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONF_BRIGHTNESS_LEVELS = "brightness_levels"
|
CONF_BRIGHTNESS_LEVELS = "brightness_levels"
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_PLATFORM): mochad.DOMAIN,
|
vol.Required(CONF_PLATFORM): DOMAIN,
|
||||||
CONF_DEVICES: [
|
CONF_DEVICES: [
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_NAME): cv.string,
|
vol.Optional(CONF_NAME): cv.string,
|
||||||
vol.Required(CONF_ADDRESS): cv.x10_address,
|
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.Optional(CONF_BRIGHTNESS_LEVELS, default=32): vol.All(
|
||||||
vol.Coerce(int), vol.In([32, 64, 256])
|
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):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up X10 dimmers over a mochad controller."""
|
"""Set up X10 dimmers over a mochad controller."""
|
||||||
devs = config.get(CONF_DEVICES)
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -46,12 +48,11 @@ class MochadLight(Light):
|
|||||||
|
|
||||||
def __init__(self, hass, ctrl, dev):
|
def __init__(self, hass, ctrl, dev):
|
||||||
"""Initialize a Mochad Light Device."""
|
"""Initialize a Mochad Light Device."""
|
||||||
from pymochad import device
|
|
||||||
|
|
||||||
self._controller = ctrl
|
self._controller = ctrl
|
||||||
self._address = dev[CONF_ADDRESS]
|
self._address = dev[CONF_ADDRESS]
|
||||||
self._name = dev.get(CONF_NAME, f"x10_light_dev_{self._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.light = device.Device(ctrl, self._address, comm_type=self._comm_type)
|
||||||
self._brightness = 0
|
self._brightness = 0
|
||||||
self._state = self._get_device_status()
|
self._state = self._get_device_status()
|
||||||
@ -64,7 +65,7 @@ class MochadLight(Light):
|
|||||||
|
|
||||||
def _get_device_status(self):
|
def _get_device_status(self):
|
||||||
"""Get the status of the light from mochad."""
|
"""Get the status of the light from mochad."""
|
||||||
with mochad.REQ_LOCK:
|
with REQ_LOCK:
|
||||||
status = self.light.get_status().rstrip()
|
status = self.light.get_status().rstrip()
|
||||||
return status == "on"
|
return status == "on"
|
||||||
|
|
||||||
@ -106,7 +107,7 @@ class MochadLight(Light):
|
|||||||
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."""
|
||||||
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
|
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
|
||||||
with mochad.REQ_LOCK:
|
with REQ_LOCK:
|
||||||
if self._brightness_levels > 32:
|
if self._brightness_levels > 32:
|
||||||
out_brightness = self._calculate_brightness_value(brightness)
|
out_brightness = self._calculate_brightness_value(brightness)
|
||||||
self.light.send_cmd(f"xdim {out_brightness}")
|
self.light.send_cmd(f"xdim {out_brightness}")
|
||||||
@ -124,7 +125,7 @@ class MochadLight(Light):
|
|||||||
|
|
||||||
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."""
|
||||||
with mochad.REQ_LOCK:
|
with REQ_LOCK:
|
||||||
self.light.send_cmd("off")
|
self.light.send_cmd("off")
|
||||||
self._controller.read_data()
|
self._controller.read_data()
|
||||||
# There is no persistence for X10 modules so we need to prepare
|
# There is no persistence for X10 modules so we need to prepare
|
||||||
|
@ -1,24 +1,27 @@
|
|||||||
"""Support for X10 switch over Mochad."""
|
"""Support for X10 switch over Mochad."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from pymochad import device
|
||||||
|
from pymochad.exceptions import MochadException
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import mochad
|
|
||||||
from homeassistant.components.switch import SwitchDevice
|
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 homeassistant.helpers import config_validation as cv
|
||||||
|
|
||||||
|
from . import CONF_COMM_TYPE, CONTROLLER, DOMAIN, REQ_LOCK
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = vol.Schema(
|
PLATFORM_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_PLATFORM): mochad.DOMAIN,
|
vol.Required(CONF_PLATFORM): DOMAIN,
|
||||||
CONF_DEVICES: [
|
CONF_DEVICES: [
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_NAME): cv.string,
|
vol.Optional(CONF_NAME): cv.string,
|
||||||
vol.Required(CONF_ADDRESS): cv.x10_address,
|
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):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up X10 switches over a mochad controller."""
|
"""Set up X10 switches over a mochad controller."""
|
||||||
devs = config.get(CONF_DEVICES)
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -37,12 +40,11 @@ class MochadSwitch(SwitchDevice):
|
|||||||
|
|
||||||
def __init__(self, hass, ctrl, dev):
|
def __init__(self, hass, ctrl, dev):
|
||||||
"""Initialize a Mochad Switch Device."""
|
"""Initialize a Mochad Switch Device."""
|
||||||
from pymochad import device
|
|
||||||
|
|
||||||
self._controller = ctrl
|
self._controller = ctrl
|
||||||
self._address = dev[CONF_ADDRESS]
|
self._address = dev[CONF_ADDRESS]
|
||||||
self._name = dev.get(CONF_NAME, "x10_switch_dev_%s" % self._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)
|
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
|
# Init with false to avoid locking HA for long on CM19A (goes from rf
|
||||||
# to pl via TM751, but not other way around)
|
# to pl via TM751, but not other way around)
|
||||||
@ -58,10 +60,9 @@ class MochadSwitch(SwitchDevice):
|
|||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
from pymochad.exceptions import MochadException
|
|
||||||
|
|
||||||
_LOGGER.debug("Reconnect %s:%s", self._controller.server, self._controller.port)
|
_LOGGER.debug("Reconnect %s:%s", self._controller.server, self._controller.port)
|
||||||
with mochad.REQ_LOCK:
|
with REQ_LOCK:
|
||||||
try:
|
try:
|
||||||
# Recycle socket on new command to recover mochad connection
|
# Recycle socket on new command to recover mochad connection
|
||||||
self._controller.reconnect()
|
self._controller.reconnect()
|
||||||
@ -75,10 +76,9 @@ class MochadSwitch(SwitchDevice):
|
|||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the switch off."""
|
"""Turn the switch off."""
|
||||||
from pymochad.exceptions import MochadException
|
|
||||||
|
|
||||||
_LOGGER.debug("Reconnect %s:%s", self._controller.server, self._controller.port)
|
_LOGGER.debug("Reconnect %s:%s", self._controller.server, self._controller.port)
|
||||||
with mochad.REQ_LOCK:
|
with REQ_LOCK:
|
||||||
try:
|
try:
|
||||||
# Recycle socket on new command to recover mochad connection
|
# Recycle socket on new command to recover mochad connection
|
||||||
self._controller.reconnect()
|
self._controller.reconnect()
|
||||||
@ -92,7 +92,7 @@ class MochadSwitch(SwitchDevice):
|
|||||||
|
|
||||||
def _get_device_status(self):
|
def _get_device_status(self):
|
||||||
"""Get the status of the switch from mochad."""
|
"""Get the status of the switch from mochad."""
|
||||||
with mochad.REQ_LOCK:
|
with REQ_LOCK:
|
||||||
status = self.switch.get_status().rstrip()
|
status = self.switch.get_status().rstrip()
|
||||||
return status == "on"
|
return status == "on"
|
||||||
|
|
||||||
|
@ -14,8 +14,8 @@ from tests.common import get_test_home_assistant
|
|||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def pymochad_mock():
|
def pymochad_mock():
|
||||||
"""Mock pymochad."""
|
"""Mock pymochad."""
|
||||||
with mock.patch.dict("sys.modules", {"pymochad": mock.MagicMock()}):
|
with mock.patch("homeassistant.components.mochad.light.device") as device:
|
||||||
yield
|
yield device
|
||||||
|
|
||||||
|
|
||||||
class TestMochadSwitchSetup(unittest.TestCase):
|
class TestMochadSwitchSetup(unittest.TestCase):
|
||||||
|
@ -14,9 +14,8 @@ from tests.common import get_test_home_assistant
|
|||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def pymochad_mock():
|
def pymochad_mock():
|
||||||
"""Mock pymochad."""
|
"""Mock pymochad."""
|
||||||
with mock.patch.dict(
|
with mock.patch("homeassistant.components.mochad.switch.device"), mock.patch(
|
||||||
"sys.modules",
|
"homeassistant.components.mochad.switch.MochadException"
|
||||||
{"pymochad": mock.MagicMock(), "pymochad.exceptions": mock.MagicMock()},
|
|
||||||
):
|
):
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user