-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Improve service by allowing to reference entity id instead of deconz id #11862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
b974607
5685ec1
e89d70e
4562701
197901c
31688e0
ba378fd
fc12bfe
df4b1f4
a324785
757e1c2
24f099f
478efd3
7e0f481
042ffe6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,8 @@ | |
| import asyncio | ||
|
|
||
| from homeassistant.components.binary_sensor import BinarySensorDevice | ||
| from homeassistant.components.deconz import DOMAIN as DECONZ_DATA | ||
| from homeassistant.components.deconz import ( | ||
| DOMAIN as DATA_DECONZ, DATA_DECONZ_ID) | ||
| from homeassistant.const import ATTR_BATTERY_LEVEL | ||
| from homeassistant.core import callback | ||
|
|
||
|
|
@@ -21,27 +22,29 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | |
| return | ||
|
|
||
| from pydeconz.sensor import DECONZ_BINARY_SENSOR | ||
| sensors = hass.data[DECONZ_DATA].sensors | ||
| sensors = hass.data[DATA_DECONZ].sensors | ||
| entities = [] | ||
|
|
||
| for key in sorted(sensors.keys(), key=int): | ||
| sensor = sensors[key] | ||
| if sensor and sensor.type in DECONZ_BINARY_SENSOR: | ||
| entities.append(DeconzBinarySensor(sensor)) | ||
| entities.append(DeconzBinarySensor(hass, sensor)) | ||
| async_add_devices(entities, True) | ||
|
|
||
|
|
||
| class DeconzBinarySensor(BinarySensorDevice): | ||
| """Representation of a binary sensor.""" | ||
|
|
||
| def __init__(self, sensor): | ||
| def __init__(self, hass, sensor): | ||
| """Set up sensor and add update callback to get data from websocket.""" | ||
| self.hass = hass | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this. |
||
| self._sensor = sensor | ||
|
|
||
| @asyncio.coroutine | ||
| def async_added_to_hass(self): | ||
| """Subscribe sensors events.""" | ||
| self._sensor.register_async_callback(self.async_update_callback) | ||
| self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._sensor.deconz_id | ||
|
|
||
| @callback | ||
| def async_update_callback(self, reason): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,10 @@ | |
| For more details about this component, please refer to the documentation at | ||
| https://home-assistant.io/components/deconz/ | ||
| """ | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from itertools import chain | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'itertools.chain' imported but unused There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'itertools.chain' imported but unused |
||
|
|
||
| import voluptuous as vol | ||
|
|
||
|
|
@@ -17,11 +19,12 @@ | |
| from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
| from homeassistant.util.json import load_json, save_json | ||
|
|
||
| REQUIREMENTS = ['pydeconz==27'] | ||
| REQUIREMENTS = ['pydeconz==28'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DOMAIN = 'deconz' | ||
| DATA_DECONZ_ID = 'deconz_entities' | ||
|
|
||
| CONFIG_FILE = 'deconz.conf' | ||
|
|
||
|
|
@@ -34,13 +37,16 @@ | |
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
| SERVICE_FIELD = 'field' | ||
| SERVICE_ENTITY = 'entity' | ||
| SERVICE_DATA = 'data' | ||
|
|
||
| SERVICE_SCHEMA = vol.Schema({ | ||
| vol.Required(SERVICE_FIELD): cv.string, | ||
| vol.Exclusive(SERVICE_FIELD, 'deconz_id'): cv.string, | ||
| vol.Exclusive(SERVICE_ENTITY, 'deconz_id'): cv.entity_id, | ||
| vol.Required(SERVICE_DATA): dict, | ||
| }) | ||
|
|
||
|
|
||
| CONFIG_INSTRUCTIONS = """ | ||
| Unlock your deCONZ gateway to register with Home Assistant. | ||
|
|
||
|
|
@@ -100,6 +106,7 @@ def async_setup_deconz(hass, config, deconz_config): | |
| return False | ||
|
|
||
| hass.data[DOMAIN] = deconz | ||
| hass.data[DATA_DECONZ_ID] = {} | ||
|
|
||
| for component in ['binary_sensor', 'light', 'scene', 'sensor']: | ||
| hass.async_add_job(discovery.async_load_platform( | ||
|
|
@@ -112,6 +119,7 @@ def async_configure(call): | |
|
|
||
| Field is a string representing a specific device in deCONZ | ||
| e.g. field='/lights/1/state'. | ||
| Entity_id can be used to retrieve the proper field. | ||
| Data is a json object with what data you want to alter | ||
| e.g. data={'on': true}. | ||
| { | ||
|
|
@@ -121,9 +129,17 @@ def async_configure(call): | |
| See Dresden Elektroniks REST API documentation for details: | ||
| http://dresden-elektronik.github.io/deconz-rest-doc/rest/ | ||
| """ | ||
| deconz = hass.data[DOMAIN] | ||
| field = call.data.get(SERVICE_FIELD) | ||
| entity_id = call.data.get(SERVICE_ENTITY) | ||
| data = call.data.get(SERVICE_DATA) | ||
| deconz = hass.data[DOMAIN] | ||
| if entity_id: | ||
| entities = hass.data.get(DATA_DECONZ_ID) | ||
| if entities: | ||
| field = entities.get(entity_id) | ||
| if field is None: | ||
| _LOGGER.error('Could not find the entity %s', entity_id) | ||
| return | ||
| yield from deconz.async_put_state(field, data) | ||
| hass.services.async_register( | ||
| DOMAIN, 'configure', async_configure, schema=SERVICE_SCHEMA) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
|
|
||
| configure: | ||
| description: Set attribute of device in Deconz. See Dresden Elektroniks REST API documentation for details http://dresden-elektronik.github.io/deconz-rest-doc/rest/ | ||
| description: Set attribute of device in deCONZ. See https://home-assistant.io/components/deconz/#device-services for details. | ||
| fields: | ||
| field: | ||
| description: Field is a string representing a specific device in Deconz. | ||
| description: Field is a string representing a specific device in deCONZ. | ||
| example: '/lights/1/state' | ||
| entity: | ||
| description: Entity id representing a specific device in deCONZ. | ||
| example: 'light.rgb_light' | ||
| data: | ||
| description: Data is a json object with what data you want to alter. | ||
| example: '{"on": true}' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ | |
| """ | ||
| import asyncio | ||
|
|
||
| from homeassistant.components.deconz import DOMAIN as DECONZ_DATA | ||
| from homeassistant.components.deconz import ( | ||
| DOMAIN as DATA_DECONZ, DATA_DECONZ_ID) | ||
| from homeassistant.components.light import ( | ||
| ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_FLASH, ATTR_RGB_COLOR, | ||
| ATTR_TRANSITION, ATTR_XY_COLOR, EFFECT_COLORLOOP, FLASH_LONG, FLASH_SHORT, | ||
|
|
@@ -17,33 +18,32 @@ | |
|
|
||
| DEPENDENCIES = ['deconz'] | ||
|
|
||
| ATTR_LIGHT_GROUP = 'LightGroup' | ||
|
|
||
|
|
||
| @asyncio.coroutine | ||
| def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | ||
| """Set up the deCONZ light.""" | ||
| if discovery_info is None: | ||
| return | ||
|
|
||
| lights = hass.data[DECONZ_DATA].lights | ||
| groups = hass.data[DECONZ_DATA].groups | ||
| lights = hass.data[DATA_DECONZ].lights | ||
| groups = hass.data[DATA_DECONZ].groups | ||
| entities = [] | ||
|
|
||
| for light in lights.values(): | ||
| entities.append(DeconzLight(light)) | ||
| entities.append(DeconzLight(hass, light)) | ||
|
|
||
| for group in groups.values(): | ||
| if group.lights: # Don't create entity for group not containing light | ||
| entities.append(DeconzLight(group)) | ||
| entities.append(DeconzLight(hass, group)) | ||
| async_add_devices(entities, True) | ||
|
|
||
|
|
||
| class DeconzLight(Light): | ||
| """Representation of a deCONZ light.""" | ||
|
|
||
| def __init__(self, light): | ||
| def __init__(self, hass, light): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't pass in |
||
| """Set up light and add update callback to get data from websocket.""" | ||
| self.hass = hass | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove. |
||
| self._light = light | ||
|
|
||
| self._features = SUPPORT_BRIGHTNESS | ||
|
|
@@ -64,6 +64,7 @@ def __init__(self, light): | |
| def async_added_to_hass(self): | ||
| """Subscribe to lights events.""" | ||
| self._light.register_async_callback(self.async_update_callback) | ||
| self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._light.deconz_id | ||
|
|
||
| @callback | ||
| def async_update_callback(self, reason): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ | |
| """ | ||
| import asyncio | ||
|
|
||
| from homeassistant.components.deconz import DOMAIN as DECONZ_DATA | ||
| from homeassistant.components.deconz import ( | ||
| DOMAIN as DATA_DECONZ, DATA_DECONZ_ID) | ||
| from homeassistant.components.scene import Scene | ||
|
|
||
| DEPENDENCIES = ['deconz'] | ||
|
|
@@ -18,21 +19,27 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | |
| if discovery_info is None: | ||
| return | ||
|
|
||
| scenes = hass.data[DECONZ_DATA].scenes | ||
| scenes = hass.data[DATA_DECONZ].scenes | ||
| entities = [] | ||
|
|
||
| for scene in scenes.values(): | ||
| entities.append(DeconzScene(scene)) | ||
| entities.append(DeconzScene(hass, scene)) | ||
| async_add_devices(entities) | ||
|
|
||
|
|
||
| class DeconzScene(Scene): | ||
| """Representation of a deCONZ scene.""" | ||
|
|
||
| def __init__(self, scene): | ||
| def __init__(self, hass, scene): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
| """Set up a scene.""" | ||
| self.hass = hass | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
| self._scene = scene | ||
|
|
||
| @asyncio.coroutine | ||
| def async_added_to_hass(self): | ||
| """Subscribe to sensors events.""" | ||
| self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._scene.deconz_id | ||
|
|
||
| @asyncio.coroutine | ||
| def async_activate(self): | ||
| """Activate the scene.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ | |
| """ | ||
| import asyncio | ||
|
|
||
| from homeassistant.components.deconz import DOMAIN as DECONZ_DATA | ||
| from homeassistant.components.deconz import ( | ||
| DOMAIN as DATA_DECONZ, DATA_DECONZ_ID) | ||
| from homeassistant.const import ATTR_BATTERY_LEVEL, CONF_EVENT, CONF_ID | ||
| from homeassistant.core import EventOrigin, callback | ||
| from homeassistant.helpers.entity import Entity | ||
|
|
@@ -25,7 +26,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | |
| return | ||
|
|
||
| from pydeconz.sensor import DECONZ_SENSOR, SWITCH as DECONZ_REMOTE | ||
| sensors = hass.data[DECONZ_DATA].sensors | ||
| sensors = hass.data[DATA_DECONZ].sensors | ||
| entities = [] | ||
|
|
||
| for key in sorted(sensors.keys(), key=int): | ||
|
|
@@ -34,23 +35,25 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | |
| if sensor.type in DECONZ_REMOTE: | ||
| DeconzEvent(hass, sensor) | ||
| if sensor.battery: | ||
| entities.append(DeconzBattery(sensor)) | ||
| entities.append(DeconzBattery(hass, sensor)) | ||
| else: | ||
| entities.append(DeconzSensor(sensor)) | ||
| entities.append(DeconzSensor(hass, sensor)) | ||
| async_add_devices(entities, True) | ||
|
|
||
|
|
||
| class DeconzSensor(Entity): | ||
| """Representation of a sensor.""" | ||
|
|
||
| def __init__(self, sensor): | ||
| def __init__(self, hass, sensor): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
| """Set up sensor and add update callback to get data from websocket.""" | ||
| self.hass = hass | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
| self._sensor = sensor | ||
|
|
||
| @asyncio.coroutine | ||
| def async_added_to_hass(self): | ||
| """Subscribe to sensors events.""" | ||
| self._sensor.register_async_callback(self.async_update_callback) | ||
| self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._sensor.deconz_id | ||
|
|
||
| @callback | ||
| def async_update_callback(self, reason): | ||
|
|
@@ -116,8 +119,9 @@ def device_state_attributes(self): | |
| class DeconzBattery(Entity): | ||
| """Battery class for when a device is only represented as an event.""" | ||
|
|
||
| def __init__(self, device): | ||
| def __init__(self, hass, device): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
| """Register dispatcher callback for update of battery state.""" | ||
| self.hass = hass | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
| self._device = device | ||
| self._name = '{} {}'.format(self._device.name, 'Battery Level') | ||
| self._device_class = 'battery' | ||
|
|
@@ -127,6 +131,7 @@ def __init__(self, device): | |
| def async_added_to_hass(self): | ||
| """Subscribe to sensors events.""" | ||
| self._device.register_async_callback(self.async_update_callback) | ||
| self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._device.deconz_id | ||
|
|
||
| @callback | ||
| def async_update_callback(self, reason): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't pass in
hass. It will be set on the entity when the entity has been added to home assistant. This means you can accesshasswithself.hass, after the entity has been added to home assistant.