Dimmer service

This commit is contained in:
Martin Bauer
2019-06-19 16:12:22 +02:00
parent 72b4d38e80
commit 20f43f4294
12 changed files with 146 additions and 45 deletions

View File

@@ -0,0 +1,70 @@
""" Service to increase/decrease light brightness"""
import logging
import voluptuous as vol
from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN as LIGHT_DOMAIN, ATTR_TRANSITION
from homeassistant.const import SERVICE_TURN_ON, ATTR_ENTITY_ID
# The domain of your component. Should be equal to the name of your component.
from homeassistant.helpers.service import async_extract_entity_ids
DOMAIN = "dimmer"
# List of component names (string) your component depends upon.
# We depend on group because group will be loaded after all the components that
# initialize devices have been setup.
DEPENDENCIES = ['group', 'light']
# Name of the service that we expose.
SERVICE_DIM = 'dim'
# Shortcut for the logger
_LOGGER = logging.getLogger(__name__)
# Validate that all required config options are given.
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({})
}, extra=vol.ALLOW_EXTRA)
async def async_setup(hass, config):
"""Setup example component."""
async def async_dim_service(service):
params = service.data.copy()
entity_ids = await async_extract_entity_ids(hass, service, expand_group=True)
offset = params.get('offset', None)
factor = params.get('factor', None)
min_brightness = params.get('min_brightness', 6)
transition = params.get(ATTR_TRANSITION, None)
if factor is None and offset is None:
offset = 50
assert not (factor is not None and offset is not None)
def clip_value(level):
level = int(level)
if level < min_brightness:
return min_brightness
if level > 255:
return 255
return level
for entity_id in entity_ids:
brightness = hass.states.get(entity_id).attributes.get('brightness', 0)
if factor is not None:
data = {ATTR_BRIGHTNESS: clip_value(brightness * factor)}
if offset is not None:
data = {ATTR_BRIGHTNESS: clip_value(brightness + offset)}
data[ATTR_ENTITY_ID] = entity_id
if transition:
data[ATTR_TRANSITION] = transition
await hass.services.async_call(LIGHT_DOMAIN, SERVICE_TURN_ON, data)
hass.services.async_register(DOMAIN, SERVICE_DIM, async_dim_service)
return True

View File

@@ -0,0 +1,17 @@
dim:
description: Increases or decreases brightness of lights
fields:
entity_id:
description: Name(s) of entities or groups
example: 'light.living_room'
offset:
description: New light value is old value plus this offset (full brightness is 255).
Can be negative to decrease brightness
example: 30
factor:
description: Factor to multiply old light value with. Use either offset or factor, not both!
example: 1.3
transition:
description: transition time in seconds
example: 1