mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Service for setting a fixed scene of Xiaomi MIIO lights (#10819)
* Service for setting a fixed scene introduced. Fixes https://github.com/syssi/philipslight/issues/6. Fixes https://github.com/home-assistant/home-assistant/issues/10458. * Service description added. * Typo fixed. * Error message updated and naming improved. * Name ("scene") of the method parameter aligned. * Hound error fixed: Spaces removed. * async_setup_platform method simplified. * Lazy loading of service descriptions. * Unused import removed.
This commit is contained in:
parent
314582ef0c
commit
b10fd172fd
@ -159,3 +159,13 @@ lifx_effect_stop:
|
|||||||
entity_id:
|
entity_id:
|
||||||
description: Name(s) of entities to stop effects on. Leave out to stop effects everywhere.
|
description: Name(s) of entities to stop effects on. Leave out to stop effects everywhere.
|
||||||
example: 'light.bedroom'
|
example: 'light.bedroom'
|
||||||
|
|
||||||
|
xiaomi_miio_set_scene:
|
||||||
|
description: Set a fixed scene.
|
||||||
|
fields:
|
||||||
|
entity_id:
|
||||||
|
description: Name of the light entity.
|
||||||
|
example: 'light.xiaomi_miio'
|
||||||
|
scene:
|
||||||
|
description: Number of the fixed scene, between 1 and 4.
|
||||||
|
example: 1
|
||||||
|
@ -13,7 +13,7 @@ import voluptuous as vol
|
|||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
PLATFORM_SCHEMA, ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS,
|
PLATFORM_SCHEMA, ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS,
|
||||||
ATTR_COLOR_TEMP, SUPPORT_COLOR_TEMP, Light, )
|
ATTR_COLOR_TEMP, SUPPORT_COLOR_TEMP, Light, ATTR_ENTITY_ID, DOMAIN, )
|
||||||
|
|
||||||
from homeassistant.const import (CONF_NAME, CONF_HOST, CONF_TOKEN, )
|
from homeassistant.const import (CONF_NAME, CONF_HOST, CONF_TOKEN, )
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
@ -21,6 +21,7 @@ from homeassistant.exceptions import PlatformNotReady
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_NAME = 'Xiaomi Philips Light'
|
DEFAULT_NAME = 'Xiaomi Philips Light'
|
||||||
|
PLATFORM = 'xiaomi_miio'
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_HOST): cv.string,
|
vol.Required(CONF_HOST): cv.string,
|
||||||
@ -36,6 +37,24 @@ CCT_MAX = 100
|
|||||||
|
|
||||||
SUCCESS = ['ok']
|
SUCCESS = ['ok']
|
||||||
ATTR_MODEL = 'model'
|
ATTR_MODEL = 'model'
|
||||||
|
ATTR_SCENE = 'scene'
|
||||||
|
|
||||||
|
SERVICE_SET_SCENE = 'xiaomi_miio_set_scene'
|
||||||
|
|
||||||
|
XIAOMI_MIIO_SERVICE_SCHEMA = vol.Schema({
|
||||||
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
||||||
|
})
|
||||||
|
|
||||||
|
SERVICE_SCHEMA_SCENE = XIAOMI_MIIO_SERVICE_SCHEMA.extend({
|
||||||
|
vol.Required(ATTR_SCENE):
|
||||||
|
vol.All(vol.Coerce(int), vol.Clamp(min=1, max=4))
|
||||||
|
})
|
||||||
|
|
||||||
|
SERVICE_TO_METHOD = {
|
||||||
|
SERVICE_SET_SCENE: {
|
||||||
|
'method': 'async_set_scene',
|
||||||
|
'schema': SERVICE_SCHEMA_SCENE}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
@ -43,6 +62,8 @@ ATTR_MODEL = 'model'
|
|||||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||||
"""Set up the light from config."""
|
"""Set up the light from config."""
|
||||||
from miio import Device, DeviceException
|
from miio import Device, DeviceException
|
||||||
|
if PLATFORM not in hass.data:
|
||||||
|
hass.data[PLATFORM] = {}
|
||||||
|
|
||||||
host = config.get(CONF_HOST)
|
host = config.get(CONF_HOST)
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
@ -50,7 +71,6 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
|
|
||||||
_LOGGER.info("Initializing with host %s (token %s...)", host, token[:5])
|
_LOGGER.info("Initializing with host %s (token %s...)", host, token[:5])
|
||||||
|
|
||||||
devices = []
|
|
||||||
try:
|
try:
|
||||||
light = Device(host, token)
|
light = Device(host, token)
|
||||||
device_info = light.info()
|
device_info = light.info()
|
||||||
@ -63,27 +83,53 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
from miio import PhilipsEyecare
|
from miio import PhilipsEyecare
|
||||||
light = PhilipsEyecare(host, token)
|
light = PhilipsEyecare(host, token)
|
||||||
device = XiaomiPhilipsEyecareLamp(name, light, device_info)
|
device = XiaomiPhilipsEyecareLamp(name, light, device_info)
|
||||||
devices.append(device)
|
|
||||||
elif device_info.model == 'philips.light.ceiling':
|
elif device_info.model == 'philips.light.ceiling':
|
||||||
from miio import Ceil
|
from miio import Ceil
|
||||||
light = Ceil(host, token)
|
light = Ceil(host, token)
|
||||||
device = XiaomiPhilipsCeilingLamp(name, light, device_info)
|
device = XiaomiPhilipsCeilingLamp(name, light, device_info)
|
||||||
devices.append(device)
|
|
||||||
elif device_info.model == 'philips.light.bulb':
|
elif device_info.model == 'philips.light.bulb':
|
||||||
from miio import PhilipsBulb
|
from miio import PhilipsBulb
|
||||||
light = PhilipsBulb(host, token)
|
light = PhilipsBulb(host, token)
|
||||||
device = XiaomiPhilipsLightBall(name, light, device_info)
|
device = XiaomiPhilipsLightBall(name, light, device_info)
|
||||||
devices.append(device)
|
|
||||||
else:
|
else:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
'Unsupported device found! Please create an issue at '
|
'Unsupported device found! Please create an issue at '
|
||||||
'https://github.com/rytilahti/python-miio/issues '
|
'https://github.com/rytilahti/python-miio/issues '
|
||||||
'and provide the following data: %s', device_info.model)
|
'and provide the following data: %s', device_info.model)
|
||||||
|
return False
|
||||||
|
|
||||||
except DeviceException:
|
except DeviceException:
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
||||||
async_add_devices(devices, update_before_add=True)
|
hass.data[PLATFORM][host] = device
|
||||||
|
async_add_devices([device], update_before_add=True)
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_service_handler(service):
|
||||||
|
"""Map services to methods on Xiaomi Philips Lights."""
|
||||||
|
method = SERVICE_TO_METHOD.get(service.service)
|
||||||
|
params = {key: value for key, value in service.data.items()
|
||||||
|
if key != ATTR_ENTITY_ID}
|
||||||
|
entity_ids = service.data.get(ATTR_ENTITY_ID)
|
||||||
|
if entity_ids:
|
||||||
|
target_devices = [dev for dev in hass.data[PLATFORM].values()
|
||||||
|
if dev.entity_id in entity_ids]
|
||||||
|
else:
|
||||||
|
target_devices = hass.data[PLATFORM].values()
|
||||||
|
|
||||||
|
update_tasks = []
|
||||||
|
for target_device in target_devices:
|
||||||
|
yield from getattr(target_device, method['method'])(**params)
|
||||||
|
update_tasks.append(target_device.async_update_ha_state(True))
|
||||||
|
|
||||||
|
if update_tasks:
|
||||||
|
yield from asyncio.wait(update_tasks, loop=hass.loop)
|
||||||
|
|
||||||
|
for xiaomi_miio_service in SERVICE_TO_METHOD:
|
||||||
|
schema = SERVICE_TO_METHOD[xiaomi_miio_service].get(
|
||||||
|
'schema', XIAOMI_MIIO_SERVICE_SCHEMA)
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN, xiaomi_miio_service, async_service_handler, schema=schema)
|
||||||
|
|
||||||
|
|
||||||
class XiaomiPhilipsGenericLight(Light):
|
class XiaomiPhilipsGenericLight(Light):
|
||||||
@ -194,6 +240,13 @@ class XiaomiPhilipsGenericLight(Light):
|
|||||||
except DeviceException as ex:
|
except DeviceException as ex:
|
||||||
_LOGGER.error("Got exception while fetching the state: %s", ex)
|
_LOGGER.error("Got exception while fetching the state: %s", ex)
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_set_scene(self, scene: int=1):
|
||||||
|
"""Set the fixed scene."""
|
||||||
|
yield from self._try_command(
|
||||||
|
"Setting a fixed scene failed.",
|
||||||
|
self._light.set_scene, scene)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def translate(value, left_min, left_max, right_min, right_max):
|
def translate(value, left_min, left_max, right_min, right_max):
|
||||||
"""Map a value from left span to right span."""
|
"""Map a value from left span to right span."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user