def async_setup_deconz(hass, config, deconz_config): """Set up a deCONZ session. Load config, group, light and sensor data for server information. Start websocket for push notification of state changes from deCONZ. """ _LOGGER.debug("deCONZ config %s", deconz_config) from pydeconz import DeconzSession websession = async_get_clientsession(hass) deconz = DeconzSession(hass.loop, websession, **deconz_config) result = yield from deconz.async_load_parameters() if result is False: _LOGGER.error("Failed to communicate with deCONZ") 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(hass, component, DOMAIN, {}, config)) deconz.start() @asyncio.coroutine def async_configure(call): """Set attribute of device in deCONZ. 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}. { "field": "/lights/1/state", "data": {"on": true} } See Dresden Elektroniks REST API documentation for details: http://dresden-elektronik.github.io/deconz-rest-doc/rest/ """ 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) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, deconz.close) return True
def async_setup_deconz(hass, config, deconz_config): """Set up a deCONZ session. Load config, group, light and sensor data for server information. Start websocket for push notification of state changes from deCONZ. """ _LOGGER.debug("deCONZ config %s", deconz_config) from pydeconz import DeconzSession websession = async_get_clientsession(hass) deconz = DeconzSession(hass.loop, websession, **deconz_config) result = yield from deconz.async_load_parameters() if result is False: _LOGGER.error("Failed to communicate with deCONZ") return False hass.data[DOMAIN] = deconz for component in ['binary_sensor', 'light', 'scene', 'sensor']: hass.async_add_job(discovery.async_load_platform( hass, component, DOMAIN, {}, config)) deconz.start() @asyncio.coroutine def async_configure(call): """Set attribute of device in deCONZ. Field is a string representing a specific device in deCONZ e.g. field='/lights/1/state'. Data is a json object with what data you want to alter e.g. data={'on': true}. { "field": "/lights/1/state", "data": {"on": true} } 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) data = call.data.get(SERVICE_DATA) yield from deconz.async_put_state(field, data) hass.services.async_register( DOMAIN, 'configure', async_configure, schema=SERVICE_SCHEMA) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, deconz.close) return True
def main(loop, **kwargs): """ """ if 'api_key' not in kwargs: api_key = yield from async_get_api_key(loop, **kwargs) kwargs['api_key'] = api_key print(api_key) websession = aiohttp.ClientSession(loop=loop) deconz = DeconzSession(loop, websession, **kwargs) result = yield from deconz.async_load_parameters() if result is False: print('Failed to setup deCONZ') return False deconz.start() from pprint import pprint for dev_id, dev in deconz.sensors.items(): pprint(dev.__dict__)
def async_setup_deconz(hass, config, deconz_config): """Set up a deCONZ session. Load config, group, light and sensor data for server information. Start websocket for push notification of state changes from deCONZ. """ _LOGGER.debug("deCONZ config %s", deconz_config) from pydeconz import DeconzSession websession = async_get_clientsession(hass) deconz = DeconzSession(hass.loop, websession, **deconz_config) result = yield from deconz.async_load_parameters() if result is False: _LOGGER.error("Failed to communicate with deCONZ") 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( hass, component, DOMAIN, {}, config)) deconz.start() @asyncio.coroutine def async_configure(call): """Set attribute of device in deCONZ. 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}. { "field": "/lights/1/state", "data": {"on": true} } See Dresden Elektroniks REST API documentation for details: http://dresden-elektronik.github.io/deconz-rest-doc/rest/ """ 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) @callback def deconz_shutdown(event): """ Wrap the call to deconz.close. Used as an argument to EventBus.async_listen_once - EventBus calls this method with the event as the first argument, which should not be passed on to deconz.close. """ deconz.close() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, deconz_shutdown) return True