From ea9c103fe1b860d65f0aba25d6c2eef196b042f9 Mon Sep 17 00:00:00 2001 From: Glenn Waters Date: Mon, 11 May 2020 23:34:31 -0400 Subject: [PATCH] Universal Powerline Bus -- Link Events (#35526) * Universal Powerline Bus - Link Event * Bump lib version. * Update homeassistant/components/upb/__init__.py Co-authored-by: J. Nick Koston Co-authored-by: J. Nick Koston --- homeassistant/components/upb/__init__.py | 30 +++++++++++++++++++++- homeassistant/components/upb/const.py | 6 ++++- homeassistant/components/upb/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/upb/__init__.py b/homeassistant/components/upb/__init__.py index 4792a380089..b84bab054a2 100644 --- a/homeassistant/components/upb/__init__.py +++ b/homeassistant/components/upb/__init__.py @@ -8,7 +8,14 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import Entity from homeassistant.helpers.typing import ConfigType -from .const import DOMAIN +from .const import ( + ATTR_ADDRESS, + ATTR_BRIGHTNESS_PCT, + ATTR_COMMAND, + ATTR_RATE, + DOMAIN, + EVENT_UPB_LINK_CHANGED, +) UPB_PLATFORMS = ["light", "scene"] @@ -34,6 +41,27 @@ async def async_setup_entry(hass, config_entry): hass.config_entries.async_forward_entry_setup(config_entry, component) ) + def _element_changed(element, changeset): + change = changeset.get("last_change") + if change is None: + return + if change.get("command") is None: + return + + hass.bus.async_fire( + EVENT_UPB_LINK_CHANGED, + { + ATTR_COMMAND: change["command"], + ATTR_ADDRESS: element.addr.index, + ATTR_BRIGHTNESS_PCT: change.get("level", -1), + ATTR_RATE: change.get("rate", -1), + }, + ) + + for link in upb.links: + element = upb.links[link] + element.add_callback(_element_changed) + return True diff --git a/homeassistant/components/upb/const.py b/homeassistant/components/upb/const.py index e978a4dc377..f01af0fd39f 100644 --- a/homeassistant/components/upb/const.py +++ b/homeassistant/components/upb/const.py @@ -4,13 +4,17 @@ import voluptuous as vol import homeassistant.helpers.config_validation as cv -CONF_NETWORK = "network" DOMAIN = "upb" +ATTR_ADDRESS = "address" ATTR_BLINK_RATE = "blink_rate" ATTR_BRIGHTNESS = "brightness" ATTR_BRIGHTNESS_PCT = "brightness_pct" +ATTR_COMMAND = "command" ATTR_RATE = "rate" +CONF_NETWORK = "network" +EVENT_UPB_LINK_CHANGED = "upb.link_changed" + VALID_BRIGHTNESS = vol.All(vol.Coerce(int), vol.Clamp(min=0, max=255)) VALID_BRIGHTNESS_PCT = vol.All(vol.Coerce(float), vol.Range(min=0, max=100)) VALID_RATE = vol.All(vol.Coerce(float), vol.Clamp(min=-1, max=3600)) diff --git a/homeassistant/components/upb/manifest.json b/homeassistant/components/upb/manifest.json index a6907408ea2..1f170030df6 100644 --- a/homeassistant/components/upb/manifest.json +++ b/homeassistant/components/upb/manifest.json @@ -2,7 +2,7 @@ "domain": "upb", "name": "Universal Powerline Bus (UPB)", "documentation": "https://www.home-assistant.io/integrations/upb", - "requirements": ["upb_lib==0.4.10"], + "requirements": ["upb_lib==0.4.11"], "codeowners": ["@gwww"], "config_flow": true } diff --git a/requirements_all.txt b/requirements_all.txt index f939105b188..ef47039adc4 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2114,7 +2114,7 @@ uEagle==0.0.1 unifiled==0.11 # homeassistant.components.upb -upb_lib==0.4.10 +upb_lib==0.4.11 # homeassistant.components.upcloud upcloud-api==0.4.5 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 65ff7e8ef38..21eed4d7d6d 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -844,7 +844,7 @@ twentemilieu==0.3.0 twilio==6.32.0 # homeassistant.components.upb -upb_lib==0.4.10 +upb_lib==0.4.11 # homeassistant.components.huawei_lte url-normalize==1.4.1