diff --git a/.coveragerc b/.coveragerc index 4a21d14e528..99da37414a2 100644 --- a/.coveragerc +++ b/.coveragerc @@ -796,6 +796,7 @@ omit = homeassistant/components/upb/__init__.py homeassistant/components/upb/const.py homeassistant/components/upb/light.py + homeassistant/components/upb/scene.py homeassistant/components/upcloud/* homeassistant/components/upnp/* homeassistant/components/upc_connect/* diff --git a/homeassistant/components/upb/__init__.py b/homeassistant/components/upb/__init__.py index 3b2d1512c61..4792a380089 100644 --- a/homeassistant/components/upb/__init__.py +++ b/homeassistant/components/upb/__init__.py @@ -10,7 +10,7 @@ from homeassistant.helpers.typing import ConfigType from .const import DOMAIN -UPB_PLATFORMS = ["light"] +UPB_PLATFORMS = ["light", "scene"] async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool: diff --git a/homeassistant/components/upb/light.py b/homeassistant/components/upb/light.py index 04a00a687da..78a983254bb 100644 --- a/homeassistant/components/upb/light.py +++ b/homeassistant/components/upb/light.py @@ -6,7 +6,7 @@ from homeassistant.components.light import ( SUPPORT_BRIGHTNESS, SUPPORT_FLASH, SUPPORT_TRANSITION, - Light, + LightEntity, ) from homeassistant.helpers import entity_platform @@ -40,7 +40,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): ) -class UpbLight(UpbAttachedEntity, Light): +class UpbLight(UpbAttachedEntity, LightEntity): """Representation of an UPB Light.""" def __init__(self, element, unique_id, upb): @@ -72,7 +72,7 @@ class UpbLight(UpbAttachedEntity, Light): await self.async_light_blink(0.5 if flash == "short" else 1.5) else: rate = kwargs.get(ATTR_TRANSITION, -1) - brightness = kwargs.get(ATTR_BRIGHTNESS, 255) / 2.55 + brightness = round(kwargs.get(ATTR_BRIGHTNESS, 255) / 2.55) self._element.turn_on(brightness, rate) async def async_turn_off(self, **kwargs): @@ -83,7 +83,7 @@ class UpbLight(UpbAttachedEntity, Light): async def async_light_fade_start(self, rate, brightness=None, brightness_pct=None): """Start dimming of device.""" if brightness is not None: - brightness_pct = brightness / 2.55 + brightness_pct = round(brightness / 2.55) self._element.fade_start(brightness_pct, rate) async def async_light_fade_stop(self): diff --git a/homeassistant/components/upb/scene.py b/homeassistant/components/upb/scene.py new file mode 100644 index 00000000000..81a2f23c3c6 --- /dev/null +++ b/homeassistant/components/upb/scene.py @@ -0,0 +1,72 @@ +"""Platform for UPB link integration.""" +from typing import Any + +from homeassistant.components.scene import Scene +from homeassistant.helpers import entity_platform + +from . import UpbEntity +from .const import DOMAIN, UPB_BLINK_RATE_SCHEMA, UPB_BRIGHTNESS_RATE_SCHEMA + +SERVICE_LINK_DEACTIVATE = "link_deactivate" +SERVICE_LINK_FADE_STOP = "link_fade_stop" +SERVICE_LINK_GOTO = "link_goto" +SERVICE_LINK_FADE_START = "link_fade_start" +SERVICE_LINK_BLINK = "link_blink" + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Set up the UPB link based on a config entry.""" + upb = hass.data[DOMAIN][config_entry.entry_id]["upb"] + unique_id = config_entry.entry_id + async_add_entities(UpbLink(upb.links[link], unique_id, upb) for link in upb.links) + + platform = entity_platform.current_platform.get() + + platform.async_register_entity_service( + SERVICE_LINK_DEACTIVATE, {}, "async_link_deactivate" + ) + platform.async_register_entity_service( + SERVICE_LINK_FADE_STOP, {}, "async_link_fade_stop" + ) + platform.async_register_entity_service( + SERVICE_LINK_GOTO, UPB_BRIGHTNESS_RATE_SCHEMA, "async_link_goto" + ) + platform.async_register_entity_service( + SERVICE_LINK_FADE_START, UPB_BRIGHTNESS_RATE_SCHEMA, "async_link_fade_start" + ) + platform.async_register_entity_service( + SERVICE_LINK_BLINK, UPB_BLINK_RATE_SCHEMA, "async_link_blink" + ) + + +class UpbLink(UpbEntity, Scene): + """Representation of an UPB Link.""" + + async def async_activate(self, **kwargs: Any) -> None: + """Activate the task.""" + self._element.activate() + + async def async_link_deactivate(self): + """Activate the task.""" + self._element.deactivate() + + async def async_link_goto(self, rate, brightness=None, brightness_pct=None): + """Activate the task.""" + if brightness is not None: + brightness_pct = round(brightness / 2.55) + self._element.goto(brightness_pct, rate) + + async def async_link_fade_start(self, rate, brightness=None, brightness_pct=None): + """Start dimming a link.""" + if brightness is not None: + brightness_pct = round(brightness / 2.55) + self._element.fade_start(brightness_pct, rate) + + async def async_link_fade_stop(self): + """Stop dimming a link.""" + self._element.fade_stop() + + async def async_link_blink(self, blink_rate): + """Blink a link.""" + blink_rate = int(blink_rate * 60) + self._element.blink(blink_rate) diff --git a/homeassistant/components/upb/services.yaml b/homeassistant/components/upb/services.yaml index a66e77c99f7..661c95ba991 100644 --- a/homeassistant/components/upb/services.yaml +++ b/homeassistant/components/upb/services.yaml @@ -30,3 +30,59 @@ light_blink: rate: description: Number of seconds between 0 and 4.25 that the link flashes on. example: 4.2 + +link_deactivate: + description: Deactivate a UPB scene. + fields: + entity_id: + description: Name(s) of scenes to deactivate + example: "scene.hygge" + +link_goto: + description: Set scene to brightness. + fields: + entity_id: + description: Name(s) of scenes to deactivate + example: "scene.hygge" + brightness: + description: Number between 0 and 255 indicating brightness, where 0 turns the scene off, 1 is the minimum brightness and 255 is the maximum brightness. + example: 120 + brightness_pct: + description: Number between 0 and 100 indicating percentage of full brightness, where 0 turns the scene off, 1 is the minimum brightness and 100 is the maximum brightness. + example: 42 + rate: + description: Rate in seconds for scene to transition to new brightness + example: 3.42 + +link_fade_start: + description: Start fading a link either up or down from current brightness. + fields: + entity_id: + description: Name(s) of links to start fading + example: "scene.party" + brightness: + description: Number between 0 and 255 indicating brightness, where 0 turns the scene off, 1 is the minimum brightness and 255 is the maximum brightness. + example: 142 + brightness_pct: + description: Number between 0 and 100 indicating percentage of full brightness, where 0 turns the scene off, 1 is the minimum brightness and 100 is the maximum brightness. + example: 42 + rate: + description: Rate in seconds for scene to transition to new brightness + example: 3.42 + +link_fade_stop: + description: Stop a link fade. + fields: + entity_id: + description: Name(s) of links to stop fadding + example: "scene.dining, scene.no_tv" + +link_blink: + description: Blink a link. + fields: + entity_id: + description: Name(s) of links to start fading + example: "scene.hygge" + blink_rate: + description: Number of seconds between 0 and 4.25 that the link flashes on. + example: 1.5 diff --git a/homeassistant/components/upb/.translations/en.json b/homeassistant/components/upb/translations/en.json similarity index 100% rename from homeassistant/components/upb/.translations/en.json rename to homeassistant/components/upb/translations/en.json