2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Ubiquiti mFi sensors."""
|
2016-02-06 21:36:41 +00:00
|
|
|
import logging
|
|
|
|
|
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
|
|
|
|
2016-09-04 02:32:35 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2016-08-20 22:40:16 +00:00
|
|
|
from homeassistant.const import (
|
2016-09-04 02:32:35 +00:00
|
|
|
CONF_PASSWORD, CONF_USERNAME, TEMP_CELSIUS, STATE_ON, STATE_OFF, CONF_HOST,
|
|
|
|
CONF_SSL, CONF_VERIFY_SSL, CONF_PORT)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-09-04 02:32:35 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-09-04 02:32:35 +00:00
|
|
|
DEFAULT_SSL = True
|
|
|
|
DEFAULT_VERIFY_SSL = True
|
|
|
|
|
2016-02-07 20:32:11 +00:00
|
|
|
DIGITS = {
|
|
|
|
'volts': 1,
|
|
|
|
'amps': 1,
|
|
|
|
'active_power': 0,
|
|
|
|
'temperature': 1,
|
|
|
|
}
|
2016-09-04 02:32:35 +00:00
|
|
|
|
2016-02-06 21:36:41 +00:00
|
|
|
SENSOR_MODELS = [
|
|
|
|
'Ubiquiti mFi-THS',
|
|
|
|
'Ubiquiti mFi-CS',
|
2018-05-29 05:51:14 +00:00
|
|
|
'Ubiquiti mFi-DS',
|
2016-02-06 21:36:41 +00:00
|
|
|
'Outlet',
|
2016-02-07 20:26:25 +00:00
|
|
|
'Input Analog',
|
|
|
|
'Input Digital',
|
2016-02-06 21:36:41 +00:00
|
|
|
]
|
2016-09-04 02:32:35 +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,
|
2016-09-17 17:29:58 +00:00
|
|
|
vol.Optional(CONF_PORT): cv.port,
|
2016-09-04 02:32:35 +00:00
|
|
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
|
|
|
})
|
2016-02-06 21:36:41 +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-08-20 22:40:16 +00:00
|
|
|
host = config.get(CONF_HOST)
|
2016-02-06 21:36:41 +00:00
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
2016-09-04 02:32:35 +00:00
|
|
|
use_tls = config.get(CONF_SSL)
|
|
|
|
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:36:41 +00:00
|
|
|
|
2016-02-25 23:48:46 +00:00
|
|
|
from mficlient.client import FailedToLogin, MFiClient
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
try:
|
2016-03-01 01:37:41 +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 16:18:47 +00:00
|
|
|
_LOGGER.error("Unable to connect to mFi: %s", str(ex))
|
2016-02-06 21:36:41 +00:00
|
|
|
return False
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(MfiSensor(port, hass)
|
|
|
|
for device in client.get_devices()
|
|
|
|
for port in device.ports.values()
|
|
|
|
if port.model in SENSOR_MODELS)
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MfiSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a mFi sensor."""
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
def __init__(self, port, hass):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-02-06 21:36:41 +00:00
|
|
|
self._port = port
|
|
|
|
self._hass = hass
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of th sensor."""
|
2016-02-06 21:36:41 +00:00
|
|
|
return self._port.label
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2016-09-07 01:04:20 +00:00
|
|
|
try:
|
|
|
|
tag = self._port.tag
|
|
|
|
except ValueError:
|
|
|
|
tag = None
|
|
|
|
if tag is None:
|
|
|
|
return STATE_OFF
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._port.model == 'Input Digital':
|
2017-07-06 03:02:16 +00:00
|
|
|
return STATE_ON if self._port.value > 0 else STATE_OFF
|
|
|
|
digits = DIGITS.get(self._port.tag, 0)
|
|
|
|
return round(self._port.value, digits)
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2016-09-07 01:04:20 +00:00
|
|
|
try:
|
|
|
|
tag = self._port.tag
|
|
|
|
except ValueError:
|
|
|
|
return 'State'
|
|
|
|
|
|
|
|
if tag == 'temperature':
|
2016-04-20 03:30:44 +00:00
|
|
|
return TEMP_CELSIUS
|
2018-07-23 08:16:05 +00:00
|
|
|
if tag == 'active_pwr':
|
2016-02-06 21:36:41 +00:00
|
|
|
return 'Watts'
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._port.model == 'Input Digital':
|
2016-02-07 20:26:25 +00:00
|
|
|
return 'State'
|
2016-09-07 01:04:20 +00:00
|
|
|
return tag
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data."""
|
2016-02-06 21:36:41 +00:00
|
|
|
self._port.refresh()
|