diff --git a/.coveragerc b/.coveragerc index f97a7524a21..3350bc359af 100644 --- a/.coveragerc +++ b/.coveragerc @@ -719,6 +719,7 @@ omit = homeassistant/components/uber/sensor.py homeassistant/components/ubus/device_tracker.py homeassistant/components/ue_smart_radio/media_player.py + homeassistant/components/unifiled/* homeassistant/components/upcloud/* homeassistant/components/upnp/* homeassistant/components/upc_connect/* diff --git a/CODEOWNERS b/CODEOWNERS index eb29ee28915..809101a5271 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -311,6 +311,7 @@ homeassistant/components/twentemilieu/* @frenck homeassistant/components/twilio_call/* @robbiet480 homeassistant/components/twilio_sms/* @robbiet480 homeassistant/components/unifi/* @kane610 +homeassistant/components/unifiled/* @florisvdk homeassistant/components/upc_connect/* @pvizeli homeassistant/components/upcloud/* @scop homeassistant/components/updater/* @home-assistant/core diff --git a/homeassistant/components/unifiled/__init__.py b/homeassistant/components/unifiled/__init__.py new file mode 100644 index 00000000000..8543cc1a8fd --- /dev/null +++ b/homeassistant/components/unifiled/__init__.py @@ -0,0 +1 @@ +"""Unifi LED Lights integration.""" diff --git a/homeassistant/components/unifiled/light.py b/homeassistant/components/unifiled/light.py new file mode 100644 index 00000000000..3dd1a8d5dc9 --- /dev/null +++ b/homeassistant/components/unifiled/light.py @@ -0,0 +1,105 @@ +"""Support for Unifi Led lights.""" +import logging + +from unifiled import unifiled +import voluptuous as vol + +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, + PLATFORM_SCHEMA, + SUPPORT_BRIGHTNESS, + Light, +) +from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME +import homeassistant.helpers.config_validation as cv + +_LOGGER = logging.getLogger(__name__) + +# Validation of the user's configuration +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=20443): vol.All(cv.port, cv.string), + } +) + + +def setup_platform(hass, config, add_entities, discovery_info=None): + """Set up the Unifi LED platform.""" + + # Assign configuration variables. + # The configuration check takes care they are present. + host = config[CONF_HOST] + port = config[CONF_PORT] + username = config[CONF_USERNAME] + password = config[CONF_PASSWORD] + + api = unifiled(host, port, username=username, password=password) + + # Verify that passed in configuration works + if not api.getloginstate(): + _LOGGER.error("Could not connect to unifiled controller") + return + + add_entities(UnifiLedLight(light, api) for light in api.getlights()) + + +class UnifiLedLight(Light): + """Representation of an unifiled Light.""" + + def __init__(self, light, api): + """Init Unifi LED Light.""" + + self._api = api + self._light = light + self._name = light["name"] + self._unique_id = light["id"] + self._state = light["status"]["output"] + self._brightness = self._api.convertfrom100to255(light["status"]["led"]) + self._features = SUPPORT_BRIGHTNESS + + @property + def name(self): + """Return the display name of this light.""" + return self._name + + @property + def brightness(self): + """Return the brightness name of this light.""" + return self._brightness + + @property + def unique_id(self): + """Return the unique id of this light.""" + return self._unique_id + + @property + def is_on(self): + """Return true if light is on.""" + return self._state + + @property + def supported_features(self): + """Return the supported features of this light.""" + return self._features + + def turn_on(self, **kwargs): + """Instruct the light to turn on.""" + self._api.setdevicebrightness( + self._unique_id, + str(self._api.convertfrom255to100(kwargs.get(ATTR_BRIGHTNESS, 255))), + ) + self._api.setdeviceoutput(self._unique_id, 1) + + def turn_off(self, **kwargs): + """Instruct the light to turn off.""" + self._api.setdeviceoutput(self._unique_id, 0) + + def update(self): + """Update the light states.""" + self._state = self._api.getlightstate(self._unique_id) + self._brightness = self._api.convertfrom100to255( + self._api.getlightbrightness(self._unique_id) + ) diff --git a/homeassistant/components/unifiled/manifest.json b/homeassistant/components/unifiled/manifest.json new file mode 100644 index 00000000000..fbf05470c6d --- /dev/null +++ b/homeassistant/components/unifiled/manifest.json @@ -0,0 +1,8 @@ +{ + "domain": "unifiled", + "name": "Unifi LED", + "documentation": "https://www.home-assistant.io/integrations/unifiled", + "dependencies": [], + "codeowners": ["@florisvdk"], + "requirements": ["unifiled==0.10"] +} diff --git a/requirements_all.txt b/requirements_all.txt index eeb9d0cba20..0bb4afc2b84 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1917,6 +1917,9 @@ twentemilieu==0.1.0 # homeassistant.components.twilio twilio==6.32.0 +# homeassistant.components.unifiled +unifiled==0.10 + # homeassistant.components.upcloud upcloud-api==0.4.3