2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Ubiquiti mFi switches."""
|
2016-02-06 21:48:31 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-05 05:14:26 +00:00
|
|
|
from mficlient.client import FailedToLogin, MFiClient
|
2016-02-25 23:48:46 +00:00
|
|
|
import requests
|
2016-09-04 02:32:35 +00:00
|
|
|
import voluptuous as vol
|
2016-02-25 23:48:46 +00:00
|
|
|
|
2019-12-05 05:14:26 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
2016-09-04 02:32:35 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
2019-12-05 05:14:26 +00:00
|
|
|
CONF_PORT,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SSL,
|
2019-12-05 05:14:26 +00:00
|
|
|
CONF_USERNAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
)
|
2016-09-04 02:32:35 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-02-06 21:48:31 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-09-04 02:32:35 +00:00
|
|
|
DEFAULT_SSL = True
|
|
|
|
DEFAULT_VERIFY_SSL = True
|
|
|
|
|
2020-01-08 20:04:33 +00:00
|
|
|
SWITCH_MODELS = ["Outlet", "Output 5v", "Output 12v", "Output 24v", "Dimmer Switch"]
|
2016-09-04 02:32:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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): cv.port,
|
|
|
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
2016-02-06 21:48:31 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up mFi sensors."""
|
2016-09-04 02:32:35 +00:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
2020-04-10 20:01:57 +00:00
|
|
|
use_tls = config[CONF_SSL]
|
2016-09-04 02:32:35 +00:00
|
|
|
verify_tls = config.get(CONF_VERIFY_SSL)
|
2017-07-06 03:02:16 +00:00
|
|
|
default_port = 6443 if use_tls else 6080
|
2016-09-04 02:32:35 +00:00
|
|
|
port = int(config.get(CONF_PORT, default_port))
|
2016-02-06 21:48:31 +00:00
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
client = MFiClient(
|
|
|
|
host, username, password, port=port, use_tls=use_tls, verify=verify_tls
|
|
|
|
)
|
2016-02-25 23:48:46 +00:00
|
|
|
except (FailedToLogin, requests.exceptions.ConnectionError) as ex:
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.error("Unable to connect to mFi: %s", str(ex))
|
2016-02-06 21:48:31 +00:00
|
|
|
return False
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
add_entities(
|
|
|
|
MfiSwitch(port)
|
|
|
|
for device in client.get_devices()
|
|
|
|
for port in device.ports.values()
|
|
|
|
if port.model in SWITCH_MODELS
|
|
|
|
)
|
2016-02-06 21:48:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MfiSwitch(SwitchDevice):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of an mFi switch-able device."""
|
|
|
|
|
2016-02-06 21:48:31 +00:00
|
|
|
def __init__(self, port):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the mFi device."""
|
2016-02-06 21:48:31 +00:00
|
|
|
self._port = port
|
2016-02-07 19:28:23 +00:00
|
|
|
self._target_state = None
|
2016-02-06 21:48:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Return the polling state."""
|
2016-02-06 21:48:31 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the unique ID of the device."""
|
2016-02-06 21:48:31 +00:00
|
|
|
return self._port.ident
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the name of the device."""
|
2016-02-06 21:48:31 +00:00
|
|
|
return self._port.label
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if the device is on."""
|
2016-02-06 21:48:31 +00:00
|
|
|
return self._port.output
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Get the latest state and update the state."""
|
2016-02-06 21:48:31 +00:00
|
|
|
self._port.refresh()
|
2016-02-07 19:28:23 +00:00
|
|
|
if self._target_state is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._port.data["output"] = float(self._target_state)
|
2016-02-07 19:28:23 +00:00
|
|
|
self._target_state = None
|
2016-02-06 21:48:31 +00:00
|
|
|
|
2017-07-06 03:02:16 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch on."""
|
2016-02-06 21:48:31 +00:00
|
|
|
self._port.control(True)
|
2016-02-07 19:28:23 +00:00
|
|
|
self._target_state = True
|
2016-02-06 21:48:31 +00:00
|
|
|
|
2017-07-06 03:02:16 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch off."""
|
2016-02-06 21:48:31 +00:00
|
|
|
self._port.control(False)
|
2016-02-07 19:28:23 +00:00
|
|
|
self._target_state = False
|
2016-02-06 21:48:31 +00:00
|
|
|
|
|
|
|
@property
|
2017-03-19 21:02:11 +00:00
|
|
|
def current_power_w(self):
|
|
|
|
"""Return the current power usage in W."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return int(self._port.data.get("active_pwr", 0))
|
2016-02-06 21:48:31 +00:00
|
|
|
|
|
|
|
@property
|
2016-02-07 06:28:29 +00:00
|
|
|
def device_state_attributes(self):
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Return the state attributes for the device."""
|
2016-02-07 06:28:29 +00:00
|
|
|
attr = {}
|
2019-07-31 19:25:30 +00:00
|
|
|
attr["volts"] = round(self._port.data.get("v_rms", 0), 1)
|
|
|
|
attr["amps"] = round(self._port.data.get("i_rms", 0), 1)
|
2016-02-06 21:48:31 +00:00
|
|
|
return attr
|