Universal Powerline Bus Scene support (#35401)

* Universal Powerline Bus Scene support.

* Omint UPB scene.py

* Fix lint errors, rename .translations, Light->LightEntity.

* Add rounding to brightness to percent calcs.
This commit is contained in:
Glenn Waters 2020-05-08 22:42:23 -04:00 committed by GitHub
parent e90feb1d59
commit 9353169203
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 5 deletions

View File

@ -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/*

View File

@ -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:

View File

@ -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):

View File

@ -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)

View File

@ -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