mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Merge pull request #1151 from kk7ds/add-mfi
Add support for Ubiquiti mFi sensors and switches
This commit is contained in:
commit
428750eeda
@ -122,6 +122,7 @@ omit =
|
||||
homeassistant/components/sensor/eliqonline.py
|
||||
homeassistant/components/sensor/forecast.py
|
||||
homeassistant/components/sensor/glances.py
|
||||
homeassistant/components/sensor/mfi.py
|
||||
homeassistant/components/sensor/netatmo.py
|
||||
homeassistant/components/sensor/onewire.py
|
||||
homeassistant/components/sensor/openweathermap.py
|
||||
@ -138,6 +139,7 @@ omit =
|
||||
homeassistant/components/switch/arest.py
|
||||
homeassistant/components/switch/edimax.py
|
||||
homeassistant/components/switch/hikvisioncam.py
|
||||
homeassistant/components/switch/mfi.py
|
||||
homeassistant/components/switch/mystrom.py
|
||||
homeassistant/components/switch/orvibo.py
|
||||
homeassistant/components/switch/rest.py
|
||||
|
108
homeassistant/components/sensor/mfi.py
Normal file
108
homeassistant/components/sensor/mfi.py
Normal file
@ -0,0 +1,108 @@
|
||||
"""
|
||||
homeassistant.components.sensor.mfi
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Support for Ubiquiti mFi Sensors.
|
||||
|
||||
Configuration:
|
||||
|
||||
sensor:
|
||||
platform: mfi
|
||||
host: ADDRESS
|
||||
port: PORT
|
||||
username: USERNAME
|
||||
password: PASSWORD
|
||||
|
||||
Variable:
|
||||
|
||||
host
|
||||
*Required
|
||||
ADDRESS is the IP or hostname of your mFi controller.
|
||||
|
||||
port
|
||||
*Optional
|
||||
PORT is the port of your mFi controller (usually 6443)
|
||||
|
||||
username
|
||||
*Required
|
||||
USERNAME is the mFi admin username
|
||||
|
||||
password
|
||||
*Required
|
||||
PASSWORD is the mFi admin user's password
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD,
|
||||
TEMP_CELCIUS)
|
||||
from homeassistant.components.sensor import DOMAIN
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers import validate_config
|
||||
|
||||
REQUIREMENTS = ['mficlient==0.2']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SENSOR_MODELS = [
|
||||
'Ubiquiti mFi-THS',
|
||||
'Ubiquiti mFi-CS',
|
||||
'Outlet',
|
||||
]
|
||||
|
||||
|
||||
# pylint: disable=unused-variable
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
""" Sets up mFi sensors. """
|
||||
|
||||
if not validate_config({DOMAIN: config},
|
||||
{DOMAIN: ['host',
|
||||
CONF_USERNAME,
|
||||
CONF_PASSWORD]},
|
||||
_LOGGER):
|
||||
_LOGGER.error('A host, username, and password are required')
|
||||
return False
|
||||
|
||||
host = config.get('host')
|
||||
port = int(config.get('port', 6443))
|
||||
username = config.get(CONF_USERNAME)
|
||||
password = config.get(CONF_PASSWORD)
|
||||
|
||||
from mficlient.client import MFiClient
|
||||
|
||||
try:
|
||||
client = MFiClient(host, username, password, port=port)
|
||||
except client.FailedToLogin as ex:
|
||||
_LOGGER.error('Unable to connect to mFi: %s', str(ex))
|
||||
return False
|
||||
|
||||
add_devices(MfiSensor(port, hass)
|
||||
for device in client.get_devices()
|
||||
for port in device.ports.values()
|
||||
if port.model in SENSOR_MODELS)
|
||||
|
||||
|
||||
class MfiSensor(Entity):
|
||||
""" An mFi sensor that exposes tag=value. """
|
||||
|
||||
def __init__(self, port, hass):
|
||||
self._port = port
|
||||
self._hass = hass
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._port.label
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
return self._port.value
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
if self._port.tag == 'temperature':
|
||||
return TEMP_CELCIUS
|
||||
elif self._port.tag == 'active_pwr':
|
||||
return 'Watts'
|
||||
return self._port.tag
|
||||
|
||||
def update(self):
|
||||
self._port.refresh()
|
121
homeassistant/components/switch/mfi.py
Normal file
121
homeassistant/components/switch/mfi.py
Normal file
@ -0,0 +1,121 @@
|
||||
"""
|
||||
homeassistant.components.switch.mfi
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Support for Ubiquiti mFi Switches.
|
||||
|
||||
Configuration:
|
||||
|
||||
sensor:
|
||||
platform: mfi
|
||||
host: ADDRESS
|
||||
port: PORT
|
||||
username: USERNAME
|
||||
password: PASSWORD
|
||||
|
||||
Variable:
|
||||
|
||||
host
|
||||
*Required
|
||||
ADDRESS is the IP or hostname of your mFi controller.
|
||||
|
||||
port
|
||||
*Optional
|
||||
PORT is the port of your mFi controller (usually 6443)
|
||||
|
||||
username
|
||||
*Required
|
||||
USERNAME is the mFi admin username
|
||||
|
||||
password
|
||||
*Required
|
||||
PASSWORD is the mFi admin user's password
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
||||
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD
|
||||
from homeassistant.helpers import validate_config
|
||||
|
||||
REQUIREMENTS = ['mficlient==0.2']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SWITCH_MODELS = [
|
||||
'Outlet',
|
||||
]
|
||||
|
||||
|
||||
# pylint: disable=unused-variable
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
""" Sets up mFi sensors. """
|
||||
|
||||
if not validate_config({DOMAIN: config},
|
||||
{DOMAIN: ['host',
|
||||
CONF_USERNAME,
|
||||
CONF_PASSWORD]},
|
||||
_LOGGER):
|
||||
_LOGGER.error('A host, username, and password are required')
|
||||
return False
|
||||
|
||||
host = config.get('host')
|
||||
port = int(config.get('port', 6443))
|
||||
username = config.get('username')
|
||||
password = config.get('password')
|
||||
|
||||
from mficlient.client import MFiClient
|
||||
|
||||
try:
|
||||
client = MFiClient(host, username, password, port=port)
|
||||
except client.FailedToLogin as ex:
|
||||
_LOGGER.error('Unable to connect to mFi: %s', str(ex))
|
||||
return False
|
||||
|
||||
add_devices(MfiSwitch(port)
|
||||
for device in client.get_devices()
|
||||
for port in device.ports.values()
|
||||
if port.model in SWITCH_MODELS)
|
||||
|
||||
|
||||
class MfiSwitch(SwitchDevice):
|
||||
""" An mFi switch-able device. """
|
||||
def __init__(self, port):
|
||||
self._port = port
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
return self._port.ident
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._port.label
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
return self._port.output
|
||||
|
||||
def update(self):
|
||||
self._port.refresh()
|
||||
|
||||
def turn_on(self):
|
||||
self._port.control(True)
|
||||
self._port.data['output'] = 1.0
|
||||
|
||||
def turn_off(self):
|
||||
self._port.control(False)
|
||||
self._port.data['output'] = 0.0
|
||||
|
||||
@property
|
||||
def current_power_mwh(self):
|
||||
return int(self._port.data.get('active_pwr', 0) * 1000)
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
attr = super().state_attributes or {}
|
||||
attr['volts'] = self._port.data.get('v_rms', 0)
|
||||
attr['amps'] = self._port.data.get('i_rms', 0)
|
||||
return attr
|
@ -105,6 +105,10 @@ liffylights==0.9.4
|
||||
# homeassistant.components.light.limitlessled
|
||||
limitlessled==1.0.0
|
||||
|
||||
# homeassistant.components.sensor.mfi
|
||||
# homeassistant.components.switch.mfi
|
||||
mficlient==0.2
|
||||
|
||||
# homeassistant.components.discovery
|
||||
netdisco==0.5.2
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user