mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 02:07:54 +00:00
Use voluptuous for mFi switch (#3168)
* Migrate to voluptuous * Take change configuration into account
This commit is contained in:
parent
8467d07a3d
commit
3c615e2319
@ -7,23 +7,30 @@ https://home-assistant.io/components/sensor.mfi/
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import DOMAIN
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_PASSWORD, CONF_USERNAME, TEMP_CELSIUS, STATE_ON, STATE_OFF, CONF_HOST)
|
CONF_PASSWORD, CONF_USERNAME, TEMP_CELSIUS, STATE_ON, STATE_OFF, CONF_HOST,
|
||||||
from homeassistant.helpers import validate_config
|
CONF_SSL, CONF_VERIFY_SSL, CONF_PORT)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
REQUIREMENTS = ['mficlient==0.3.0']
|
REQUIREMENTS = ['mficlient==0.3.0']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DEFAULT_PORT = 6443
|
||||||
|
DEFAULT_SSL = True
|
||||||
|
DEFAULT_VERIFY_SSL = True
|
||||||
|
|
||||||
DIGITS = {
|
DIGITS = {
|
||||||
'volts': 1,
|
'volts': 1,
|
||||||
'amps': 1,
|
'amps': 1,
|
||||||
'active_power': 0,
|
'active_power': 0,
|
||||||
'temperature': 1,
|
'temperature': 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
SENSOR_MODELS = [
|
SENSOR_MODELS = [
|
||||||
'Ubiquiti mFi-THS',
|
'Ubiquiti mFi-THS',
|
||||||
'Ubiquiti mFi-CS',
|
'Ubiquiti mFi-CS',
|
||||||
@ -31,28 +38,27 @@ SENSOR_MODELS = [
|
|||||||
'Input Analog',
|
'Input Analog',
|
||||||
'Input Digital',
|
'Input Digital',
|
||||||
]
|
]
|
||||||
CONF_TLS = 'use_tls'
|
|
||||||
CONF_VERIFY_TLS = 'verify_tls'
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Required(CONF_HOST): cv.string,
|
||||||
|
vol.Required(CONF_USERNAME): cv.string,
|
||||||
|
vol.Required(CONF_PASSWORD): cv.string,
|
||||||
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||||
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
||||||
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-variable
|
# pylint: disable=unused-variable
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup mFi sensors."""
|
"""Setup mFi sensors."""
|
||||||
if not validate_config({DOMAIN: config},
|
|
||||||
{DOMAIN: [CONF_HOST,
|
|
||||||
CONF_USERNAME,
|
|
||||||
CONF_PASSWORD]},
|
|
||||||
_LOGGER):
|
|
||||||
_LOGGER.error('A host, username, and password are required')
|
|
||||||
return False
|
|
||||||
|
|
||||||
host = config.get(CONF_HOST)
|
host = config.get(CONF_HOST)
|
||||||
username = config.get(CONF_USERNAME)
|
username = config.get(CONF_USERNAME)
|
||||||
password = config.get(CONF_PASSWORD)
|
password = config.get(CONF_PASSWORD)
|
||||||
use_tls = bool(config.get(CONF_TLS, True))
|
use_tls = config.get(CONF_SSL)
|
||||||
verify_tls = bool(config.get(CONF_VERIFY_TLS, True))
|
verify_tls = config.get(CONF_VERIFY_SSL)
|
||||||
default_port = use_tls and 6443 or 6080
|
default_port = use_tls and DEFAULT_PORT or 6080
|
||||||
port = int(config.get('port', default_port))
|
port = int(config.get(CONF_PORT, default_port))
|
||||||
|
|
||||||
from mficlient.client import FailedToLogin, MFiClient
|
from mficlient.client import FailedToLogin, MFiClient
|
||||||
|
|
||||||
|
@ -7,43 +7,49 @@ https://home-assistant.io/components/switch.mfi/
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import (
|
||||||
from homeassistant.helpers import validate_config
|
CONF_HOST, CONF_PORT, CONF_PASSWORD, CONF_USERNAME, CONF_SSL,
|
||||||
|
CONF_VERIFY_SSL)
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
REQUIREMENTS = ['mficlient==0.3.0']
|
REQUIREMENTS = ['mficlient==0.3.0']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DEFAULT_PORT = 6443
|
||||||
|
DEFAULT_SSL = True
|
||||||
|
DEFAULT_VERIFY_SSL = True
|
||||||
|
|
||||||
SWITCH_MODELS = [
|
SWITCH_MODELS = [
|
||||||
'Outlet',
|
'Outlet',
|
||||||
'Output 5v',
|
'Output 5v',
|
||||||
'Output 12v',
|
'Output 12v',
|
||||||
'Output 24v',
|
'Output 24v',
|
||||||
]
|
]
|
||||||
CONF_TLS = 'use_tls'
|
|
||||||
CONF_VERIFY_TLS = 'verify_tls'
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Required(CONF_HOST): cv.string,
|
||||||
|
vol.Required(CONF_USERNAME): cv.string,
|
||||||
|
vol.Required(CONF_PASSWORD): cv.string,
|
||||||
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||||
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
||||||
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-variable
|
# pylint: disable=unused-variable
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup mFi sensors."""
|
"""Setup mFi sensors."""
|
||||||
if not validate_config({DOMAIN: config},
|
host = config.get(CONF_HOST)
|
||||||
{DOMAIN: ['host',
|
username = config.get(CONF_USERNAME)
|
||||||
CONF_USERNAME,
|
password = config.get(CONF_PASSWORD)
|
||||||
CONF_PASSWORD]},
|
use_tls = config.get(CONF_SSL)
|
||||||
_LOGGER):
|
verify_tls = config.get(CONF_VERIFY_SSL)
|
||||||
_LOGGER.error('A host, username, and password are required')
|
default_port = use_tls and DEFAULT_PORT or 6080
|
||||||
return False
|
port = int(config.get(CONF_PORT, default_port))
|
||||||
|
|
||||||
host = config.get('host')
|
|
||||||
username = config.get('username')
|
|
||||||
password = config.get('password')
|
|
||||||
use_tls = bool(config.get(CONF_TLS, True))
|
|
||||||
verify_tls = bool(config.get(CONF_VERIFY_TLS, True))
|
|
||||||
default_port = use_tls and 6443 or 6080
|
|
||||||
port = int(config.get('port', default_port))
|
|
||||||
|
|
||||||
from mficlient.client import FailedToLogin, MFiClient
|
from mficlient.client import FailedToLogin, MFiClient
|
||||||
|
|
||||||
|
@ -24,16 +24,14 @@ class TestMfiSensorSetup(unittest.TestCase):
|
|||||||
'port': 6123,
|
'port': 6123,
|
||||||
'username': 'user',
|
'username': 'user',
|
||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
'use_tls': True,
|
'ssl': True,
|
||||||
'verify_tls': True,
|
'verify_ssl': True,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def setup_method(self, method):
|
def setup_method(self, method):
|
||||||
"""Setup things to be run when tests are started."""
|
"""Setup things to be run when tests are started."""
|
||||||
self.hass = get_test_home_assistant()
|
self.hass = get_test_home_assistant()
|
||||||
self.hass.config.latitude = 32.87336
|
|
||||||
self.hass.config.longitude = 117.22743
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
def teardown_method(self, method):
|
||||||
"""Stop everything that was started."""
|
"""Stop everything that was started."""
|
||||||
@ -54,9 +52,8 @@ class TestMfiSensorSetup(unittest.TestCase):
|
|||||||
mock_client.FailedToLogin = Exception()
|
mock_client.FailedToLogin = Exception()
|
||||||
mock_client.MFiClient.side_effect = mock_client.FailedToLogin
|
mock_client.MFiClient.side_effect = mock_client.FailedToLogin
|
||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
self.PLATFORM.setup_platform(self.hass,
|
self.PLATFORM.setup_platform(
|
||||||
dict(self.GOOD_CONFIG),
|
self.hass, dict(self.GOOD_CONFIG), None))
|
||||||
None))
|
|
||||||
|
|
||||||
@mock.patch('mficlient.client')
|
@mock.patch('mficlient.client')
|
||||||
def test_setup_failed_connect(self, mock_client):
|
def test_setup_failed_connect(self, mock_client):
|
||||||
@ -64,9 +61,8 @@ class TestMfiSensorSetup(unittest.TestCase):
|
|||||||
mock_client.FailedToLogin = Exception()
|
mock_client.FailedToLogin = Exception()
|
||||||
mock_client.MFiClient.side_effect = requests.exceptions.ConnectionError
|
mock_client.MFiClient.side_effect = requests.exceptions.ConnectionError
|
||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
self.PLATFORM.setup_platform(self.hass,
|
self.PLATFORM.setup_platform(
|
||||||
dict(self.GOOD_CONFIG),
|
self.hass, dict(self.GOOD_CONFIG), None))
|
||||||
None))
|
|
||||||
|
|
||||||
@mock.patch('mficlient.client.MFiClient')
|
@mock.patch('mficlient.client.MFiClient')
|
||||||
def test_setup_minimum(self, mock_client):
|
def test_setup_minimum(self, mock_client):
|
||||||
@ -74,9 +70,8 @@ class TestMfiSensorSetup(unittest.TestCase):
|
|||||||
config = dict(self.GOOD_CONFIG)
|
config = dict(self.GOOD_CONFIG)
|
||||||
del config[self.THING]['port']
|
del config[self.THING]['port']
|
||||||
assert self.COMPONENT.setup(self.hass, config)
|
assert self.COMPONENT.setup(self.hass, config)
|
||||||
mock_client.assert_called_once_with('foo', 'user', 'pass',
|
mock_client.assert_called_once_with(
|
||||||
port=6443, use_tls=True,
|
'foo', 'user', 'pass', port=6443, use_tls=True, verify=True)
|
||||||
verify=True)
|
|
||||||
|
|
||||||
@mock.patch('mficlient.client.MFiClient')
|
@mock.patch('mficlient.client.MFiClient')
|
||||||
def test_setup_with_port(self, mock_client):
|
def test_setup_with_port(self, mock_client):
|
||||||
@ -84,21 +79,19 @@ class TestMfiSensorSetup(unittest.TestCase):
|
|||||||
config = dict(self.GOOD_CONFIG)
|
config = dict(self.GOOD_CONFIG)
|
||||||
config[self.THING]['port'] = 6123
|
config[self.THING]['port'] = 6123
|
||||||
assert self.COMPONENT.setup(self.hass, config)
|
assert self.COMPONENT.setup(self.hass, config)
|
||||||
mock_client.assert_called_once_with('foo', 'user', 'pass',
|
mock_client.assert_called_once_with(
|
||||||
port=6123, use_tls=True,
|
'foo', 'user', 'pass', port=6123, use_tls=True, verify=True)
|
||||||
verify=True)
|
|
||||||
|
|
||||||
@mock.patch('mficlient.client.MFiClient')
|
@mock.patch('mficlient.client.MFiClient')
|
||||||
def test_setup_with_tls_disabled(self, mock_client):
|
def test_setup_with_tls_disabled(self, mock_client):
|
||||||
"""Test setup without TLS."""
|
"""Test setup without TLS."""
|
||||||
config = dict(self.GOOD_CONFIG)
|
config = dict(self.GOOD_CONFIG)
|
||||||
del config[self.THING]['port']
|
del config[self.THING]['port']
|
||||||
config[self.THING]['use_tls'] = False
|
config[self.THING]['ssl'] = False
|
||||||
config[self.THING]['verify_tls'] = False
|
config[self.THING]['verify_ssl'] = False
|
||||||
assert self.COMPONENT.setup(self.hass, config)
|
assert self.COMPONENT.setup(self.hass, config)
|
||||||
mock_client.assert_called_once_with('foo', 'user', 'pass',
|
mock_client.assert_called_once_with(
|
||||||
port=6080, use_tls=False,
|
'foo', 'user', 'pass', port=6080, use_tls=False, verify=False)
|
||||||
verify=False)
|
|
||||||
|
|
||||||
@mock.patch('mficlient.client.MFiClient')
|
@mock.patch('mficlient.client.MFiClient')
|
||||||
@mock.patch('homeassistant.components.sensor.mfi.MfiSensor')
|
@mock.patch('homeassistant.components.sensor.mfi.MfiSensor')
|
||||||
@ -123,8 +116,6 @@ class TestMfiSensor(unittest.TestCase):
|
|||||||
def setup_method(self, method):
|
def setup_method(self, method):
|
||||||
"""Setup things to be run when tests are started."""
|
"""Setup things to be run when tests are started."""
|
||||||
self.hass = get_test_home_assistant()
|
self.hass = get_test_home_assistant()
|
||||||
self.hass.config.latitude = 32.87336
|
|
||||||
self.hass.config.longitude = 117.22743
|
|
||||||
self.port = mock.MagicMock()
|
self.port = mock.MagicMock()
|
||||||
self.sensor = mfi.MfiSensor(self.port, self.hass)
|
self.sensor = mfi.MfiSensor(self.port, self.hass)
|
||||||
|
|
||||||
|
@ -22,6 +22,8 @@ class TestMfiSwitchSetup(test_mfi_sensor.TestMfiSensorSetup):
|
|||||||
'port': 6123,
|
'port': 6123,
|
||||||
'username': 'user',
|
'username': 'user',
|
||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
|
'ssl': True,
|
||||||
|
'verify_ssl': True,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,8 +50,6 @@ class TestMfiSwitch(unittest.TestCase):
|
|||||||
def setup_method(self, method):
|
def setup_method(self, method):
|
||||||
"""Setup things to be run when tests are started."""
|
"""Setup things to be run when tests are started."""
|
||||||
self.hass = get_test_home_assistant()
|
self.hass = get_test_home_assistant()
|
||||||
self.hass.config.latitude = 32.87336
|
|
||||||
self.hass.config.longitude = 117.22743
|
|
||||||
self.port = mock.MagicMock()
|
self.port = mock.MagicMock()
|
||||||
self.switch = mfi.MfiSwitch(self.port)
|
self.switch = mfi.MfiSwitch(self.port)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user