Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Ubiquiti mFi sensors and switches #1151

Merged
merged 2 commits into from
Feb 7, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add support for Ubiquiti mFi sensors
This adds support for sensors based on Ubiquiti's mFi platform.
All ports/sensors are detected from the mFi controller and exposed.
  • Loading branch information
kk7ds committed Feb 7, 2016
commit a147304be9be58f1342425a2a68f5fbf07e2d0a3
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,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
Expand Down
108 changes: 108 additions & 0 deletions homeassistant/components/sensor/mfi.py
Original file line number Diff line number Diff line change
@@ -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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please check if the mandatory configuration parts are provided? If not, the setup should fail with a log entry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, forgot to do this, will fix it up.

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()
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ liffylights==0.9.4
# homeassistant.components.light.limitlessled
limitlessled==1.0.0

# homeassistant.components.sensor.mfi
mficlient==0.2

# homeassistant.components.discovery
netdisco==0.5.2

Expand Down