class TelldusLiveData(object): """ Gets the latest data and update the states. """ def __init__(self, hass, config): public_key = config[DOMAIN].get(CONF_PUBLIC_KEY) private_key = config[DOMAIN].get(CONF_PRIVATE_KEY) token = config[DOMAIN].get(CONF_TOKEN) token_secret = config[DOMAIN].get(CONF_TOKEN_SECRET) from tellive.client import LiveClient self._switches = {} self._sensors = {} self._hass = hass self._config = config self._client = LiveClient(public_key=public_key, private_key=private_key, access_token=token, access_secret=token_secret) def validate_session(self): """ Make a dummy request to see if the session is valid """ try: return 'email' in self.request("user/profile") except RuntimeError: return False def discover(self): """ Update states, will trigger discover """ self.update_sensors() self.update_switches() def _discover(self, found_devices, component_name): """ Send discovery event if component not yet discovered """ if not len(found_devices): return _LOGGER.info("discovered %d new %s devices", len(found_devices), component_name) component = get_component(component_name) bootstrap.setup_component(self._hass, component.DOMAIN, self._config) discovery_type = DISCOVERY_TYPES[component_name] self._hass.bus.fire(EVENT_PLATFORM_DISCOVERED, { ATTR_SERVICE: discovery_type, ATTR_DISCOVERED: found_devices }) def request(self, what, **params): """ Sends a request to the tellstick live API """ from tellive.live import const supported_methods = const.TELLSTICK_TURNON \ | const.TELLSTICK_TURNOFF \ | const.TELLSTICK_TOGGLE \ # Tellstick device methods not yet supported # | const.TELLSTICK_BELL \ # | const.TELLSTICK_DIM \ # | const.TELLSTICK_LEARN \ # | const.TELLSTICK_EXECUTE \ # | const.TELLSTICK_UP \ # | const.TELLSTICK_DOWN \ # | const.TELLSTICK_STOP default_params = { 'supportedMethods': supported_methods, "includeValues": 1, "includeScale": 1, "includeIgnored": 0 } params.update(default_params) # room for improvement: the telllive library doesn't seem to # re-use sessions, instead it opens a new session for each request # this needs to be fixed response = self._client.request(what, params) _LOGGER.debug("got response %s", response) return response def update_devices(self, local_devices, remote_devices, component_name): """ update local device list and discover new devices """ if remote_devices is None: return local_devices remote_ids = remote_devices.keys() local_ids = local_devices.keys() added_devices = list(remote_ids - local_ids) self._discover(added_devices, component_name) removed_devices = list(local_ids - remote_ids) remote_devices.update({ id: dict(local_devices[id], offline=True) for id in removed_devices }) return remote_devices def update_sensors(self): """ update local list of sensors """ try: self._sensors = self.update_devices(self._sensors, request_sensors(), "sensor") except OSError: _LOGGER.warning("could not update sensors") def update_switches(self): """ update local list of switches """ try: self._switches = self.update_devices(self._switches, request_switches(), "switch") except OSError: _LOGGER.warning("could not update switches") def _check_request(self, what, **params): """ Make request, check result if successful """ response = self.request(what, **params) return response and response.get('status') == 'success' def get_switch(self, switch_id): """ return switch representation """ return self._switches[switch_id] def get_sensor(self, sensor_id): """ return sensor representation """ return self._sensors[sensor_id] def turn_switch_on(self, switch_id): """ Turn switch off. """ if self._check_request("device/turnOn", id=switch_id): from tellive.live import const self.get_switch(switch_id)["state"] = const.TELLSTICK_TURNON def turn_switch_off(self, switch_id): """ Turn switch on. """ if self._check_request("device/turnOff", id=switch_id): from tellive.live import const self.get_switch(switch_id)["state"] = const.TELLSTICK_TURNOFF
class TelldusLiveData(object): """Get the latest data and update the states.""" def __init__(self, hass, config): """Initialize the Tellus data object.""" public_key = config[DOMAIN].get(CONF_PUBLIC_KEY) private_key = config[DOMAIN].get(CONF_PRIVATE_KEY) token = config[DOMAIN].get(CONF_TOKEN) token_secret = config[DOMAIN].get(CONF_TOKEN_SECRET) from tellive.client import LiveClient self._switches = {} self._sensors = {} self._hass = hass self._config = config self._client = LiveClient(public_key=public_key, private_key=private_key, access_token=token, access_secret=token_secret) def validate_session(self): """Make a dummy request to see if the session is valid.""" response = self.request("user/profile") return response and 'email' in response def discover(self): """Update states, will trigger discover.""" self.update_sensors() self.update_switches() def _discover(self, found_devices, component_name): """Send discovery event if component not yet discovered.""" if not found_devices: return _LOGGER.info("discovered %d new %s devices", len(found_devices), component_name) discovery.load_platform(self._hass, component_name, DOMAIN, found_devices, self._config) def request(self, what, **params): """Send a request to the Tellstick Live API.""" from tellive.live import const supported_methods = const.TELLSTICK_TURNON \ | const.TELLSTICK_TURNOFF \ | const.TELLSTICK_TOGGLE \ # Tellstick device methods not yet supported # | const.TELLSTICK_BELL \ # | const.TELLSTICK_DIM \ # | const.TELLSTICK_LEARN \ # | const.TELLSTICK_EXECUTE \ # | const.TELLSTICK_UP \ # | const.TELLSTICK_DOWN \ # | const.TELLSTICK_STOP default_params = { 'supportedMethods': supported_methods, 'includeValues': 1, 'includeScale': 1, 'includeIgnored': 0 } params.update(default_params) # room for improvement: the telllive library doesn't seem to # re-use sessions, instead it opens a new session for each request # this needs to be fixed try: response = self._client.request(what, params) _LOGGER.debug("got response %s", response) return response except OSError as error: _LOGGER.error("failed to make request to Tellduslive servers: %s", error) return None def update_devices(self, local_devices, remote_devices, component_name): """Update local device list and discover new devices.""" if remote_devices is None: return local_devices remote_ids = remote_devices.keys() local_ids = local_devices.keys() added_devices = list(remote_ids - local_ids) self._discover(added_devices, component_name) removed_devices = list(local_ids - remote_ids) remote_devices.update({ id: dict(local_devices[id], offline=True) for id in removed_devices }) return remote_devices def update_sensors(self): """Update local list of sensors.""" self._sensors = self.update_devices(self._sensors, request_sensors(), 'sensor') def update_switches(self): """Update local list of switches.""" self._switches = self.update_devices(self._switches, request_switches(), 'switch') def _check_request(self, what, **params): """Make request, check result if successful.""" response = self.request(what, **params) return response and response.get('status') == 'success' def get_switch(self, switch_id): """Return the switch representation.""" return self._switches[switch_id] def get_sensor(self, sensor_id): """Return the sensor representation.""" return self._sensors[sensor_id] def turn_switch_on(self, switch_id): """Turn switch off.""" if self._check_request('device/turnOn', id=switch_id): from tellive.live import const self.get_switch(switch_id)['state'] = const.TELLSTICK_TURNON def turn_switch_off(self, switch_id): """Turn switch on.""" if self._check_request('device/turnOff', id=switch_id): from tellive.live import const self.get_switch(switch_id)['state'] = const.TELLSTICK_TURNOFF
class TelldusLiveData(object): """ Gets the latest data and update the states. """ def __init__(self, hass, config): public_key = config[DOMAIN].get(CONF_PUBLIC_KEY) private_key = config[DOMAIN].get(CONF_PRIVATE_KEY) token = config[DOMAIN].get(CONF_TOKEN) token_secret = config[DOMAIN].get(CONF_TOKEN_SECRET) from tellive.client import LiveClient from tellive.live import TelldusLive self._sensors = [] self._switches = [] self._client = LiveClient(public_key=public_key, private_key=private_key, access_token=token, access_secret=token_secret) self._api = TelldusLive(self._client) def update(self, hass, config): """ Send discovery event if component not yet discovered """ self._update_sensors() self._update_switches() for component_name, found_devices, discovery_type in \ (('sensor', self._sensors, DISCOVER_SENSORS), ('switch', self._switches, DISCOVER_SWITCHES)): if len(found_devices): component = get_component(component_name) bootstrap.setup_component(hass, component.DOMAIN, config) hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {ATTR_SERVICE: discovery_type, ATTR_DISCOVERED: {}}) def _request(self, what, **params): """ Sends a request to the tellstick live API """ from tellive.live import const supported_methods = const.TELLSTICK_TURNON \ | const.TELLSTICK_TURNOFF \ | const.TELLSTICK_TOGGLE default_params = {'supportedMethods': supported_methods, "includeValues": 1, "includeScale": 1} params.update(default_params) # room for improvement: the telllive library doesn't seem to # re-use sessions, instead it opens a new session for each request # this needs to be fixed response = self._client.request(what, params) return response def check_request(self, what, **params): """ Make request, check result if successful """ response = self._request(what, **params) return response['status'] == "success" def validate_session(self): """ Make a dummy request to see if the session is valid """ try: response = self._request("user/profile") return 'email' in response except RuntimeError: return False @Throttle(MIN_TIME_BETWEEN_UPDATES) def _update_sensors(self): """ Get the latest sensor data from Telldus Live """ _LOGGER.info("Updating sensors from Telldus Live") self._sensors = self._request("sensors/list")["sensor"] def _update_switches(self): """ Get the configured switches from Telldus Live""" _LOGGER.info("Updating switches from Telldus Live") self._switches = self._request("devices/list")["device"] # filter out any group of switches self._switches = [switch for switch in self._switches if switch["type"] == "device"] def get_sensors(self): """ Get the configured sensors """ self._update_sensors() return self._sensors def get_switches(self): """ Get the configured switches """ self._update_switches() return self._switches def get_sensor_value(self, sensor_id, sensor_name): """ Get the latest (possibly cached) sensor value """ self._update_sensors() for component in self._sensors: if component["id"] == sensor_id: for sensor in component["data"]: if sensor["name"] == sensor_name: return (sensor["value"], component["battery"], component["lastUpdated"]) def get_switch_state(self, switch_id): """ returns state of switch. """ _LOGGER.info("Updating switch state from Telldus Live") response = self._request("device/info", id=switch_id)["state"] return int(response) def turn_switch_on(self, switch_id): """ turn switch off """ return self.check_request("device/turnOn", id=switch_id) def turn_switch_off(self, switch_id): """ turn switch on """ return self.check_request("device/turnOff", id=switch_id)
class TelldusLiveData(object): """Get the latest data and update the states.""" def __init__(self, hass, config): """Initialize the Tellus data object.""" public_key = config[DOMAIN].get(CONF_PUBLIC_KEY) private_key = config[DOMAIN].get(CONF_PRIVATE_KEY) token = config[DOMAIN].get(CONF_TOKEN) token_secret = config[DOMAIN].get(CONF_TOKEN_SECRET) from tellive.client import LiveClient self._switches = {} self._sensors = {} self._hass = hass self._config = config self._client = LiveClient( public_key=public_key, private_key=private_key, access_token=token, access_secret=token_secret) def validate_session(self): """Make a dummy request to see if the session is valid.""" response = self.request("user/profile") return response and 'email' in response def discover(self): """Update states, will trigger discover.""" self.update_sensors() self.update_switches() def _discover(self, found_devices, component_name): """Send discovery event if component not yet discovered.""" if not len(found_devices): return _LOGGER.info("discovered %d new %s devices", len(found_devices), component_name) discovery.load_platform(self._hass, component_name, DOMAIN, found_devices, self._config) def request(self, what, **params): """Send a request to the Tellstick Live API.""" from tellive.live import const supported_methods = const.TELLSTICK_TURNON \ | const.TELLSTICK_TURNOFF \ | const.TELLSTICK_TOGGLE \ # Tellstick device methods not yet supported # | const.TELLSTICK_BELL \ # | const.TELLSTICK_DIM \ # | const.TELLSTICK_LEARN \ # | const.TELLSTICK_EXECUTE \ # | const.TELLSTICK_UP \ # | const.TELLSTICK_DOWN \ # | const.TELLSTICK_STOP default_params = {'supportedMethods': supported_methods, 'includeValues': 1, 'includeScale': 1, 'includeIgnored': 0} params.update(default_params) # room for improvement: the telllive library doesn't seem to # re-use sessions, instead it opens a new session for each request # this needs to be fixed try: response = self._client.request(what, params) _LOGGER.debug("got response %s", response) return response except (ConnectionError, TimeoutError): _LOGGER.error("failed to make request to Tellduslive servers") return None def update_devices(self, local_devices, remote_devices, component_name): """Update local device list and discover new devices.""" if remote_devices is None: return local_devices remote_ids = remote_devices.keys() local_ids = local_devices.keys() added_devices = list(remote_ids - local_ids) self._discover(added_devices, component_name) removed_devices = list(local_ids - remote_ids) remote_devices.update({id: dict(local_devices[id], offline=True) for id in removed_devices}) return remote_devices def update_sensors(self): """Update local list of sensors.""" self._sensors = self.update_devices( self._sensors, request_sensors(), 'sensor') def update_switches(self): """Update local list of switches.""" self._switches = self.update_devices( self._switches, request_switches(), 'switch') def _check_request(self, what, **params): """Make request, check result if successful.""" response = self.request(what, **params) return response and response.get('status') == 'success' def get_switch(self, switch_id): """Return the switch representation.""" return self._switches[switch_id] def get_sensor(self, sensor_id): """Return the sensor representation.""" return self._sensors[sensor_id] def turn_switch_on(self, switch_id): """Turn switch off.""" if self._check_request('device/turnOn', id=switch_id): from tellive.live import const self.get_switch(switch_id)['state'] = const.TELLSTICK_TURNON def turn_switch_off(self, switch_id): """Turn switch on.""" if self._check_request('device/turnOff', id=switch_id): from tellive.live import const self.get_switch(switch_id)['state'] = const.TELLSTICK_TURNOFF
class TelldusLiveData(object): """ Gets the latest data and update the states. """ def __init__(self, hass, config): public_key = config[DOMAIN].get(CONF_PUBLIC_KEY) private_key = config[DOMAIN].get(CONF_PRIVATE_KEY) token = config[DOMAIN].get(CONF_TOKEN) token_secret = config[DOMAIN].get(CONF_TOKEN_SECRET) from tellive.client import LiveClient self._switches = {} self._sensors = {} self._hass = hass self._config = config self._client = LiveClient(public_key=public_key, private_key=private_key, access_token=token, access_secret=token_secret) def validate_session(self): """ Make a dummy request to see if the session is valid """ try: return 'email' in self.request("user/profile") except RuntimeError: return False def discover(self): """ Update states, will trigger discover """ self.update_sensors() self.update_switches() def _discover(self, found_devices, component_name): """ Send discovery event if component not yet discovered """ if not len(found_devices): return _LOGGER.info("discovered %d new %s devices", len(found_devices), component_name) component = get_component(component_name) bootstrap.setup_component(self._hass, component.DOMAIN, self._config) discovery_type = DISCOVERY_TYPES[component_name] self._hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {ATTR_SERVICE: discovery_type, ATTR_DISCOVERED: found_devices}) def request(self, what, **params): """ Sends a request to the tellstick live API """ from tellive.live import const supported_methods = const.TELLSTICK_TURNON \ | const.TELLSTICK_TURNOFF \ | const.TELLSTICK_TOGGLE \ # Tellstick device methods not yet supported # | const.TELLSTICK_BELL \ # | const.TELLSTICK_DIM \ # | const.TELLSTICK_LEARN \ # | const.TELLSTICK_EXECUTE \ # | const.TELLSTICK_UP \ # | const.TELLSTICK_DOWN \ # | const.TELLSTICK_STOP default_params = {'supportedMethods': supported_methods, "includeValues": 1, "includeScale": 1, "includeIgnored": 0} params.update(default_params) # room for improvement: the telllive library doesn't seem to # re-use sessions, instead it opens a new session for each request # this needs to be fixed response = self._client.request(what, params) _LOGGER.debug("got response %s", response) return response def update_devices(self, local_devices, remote_devices, component_name): """ update local device list and discover new devices """ if remote_devices is None: return local_devices remote_ids = remote_devices.keys() local_ids = local_devices.keys() added_devices = list(remote_ids - local_ids) self._discover(added_devices, component_name) removed_devices = list(local_ids - remote_ids) remote_devices.update({id: dict(local_devices[id], offline=True) for id in removed_devices}) return remote_devices def update_sensors(self): """ update local list of sensors """ try: self._sensors = self.update_devices(self._sensors, request_sensors(), "sensor") except OSError: _LOGGER.warning("could not update sensors") def update_switches(self): """ update local list of switches """ try: self._switches = self.update_devices(self._switches, request_switches(), "switch") except OSError: _LOGGER.warning("could not update switches") def _check_request(self, what, **params): """ Make request, check result if successful """ response = self.request(what, **params) return response and response.get('status') == 'success' def get_switch(self, switch_id): """ return switch representation """ return self._switches[switch_id] def get_sensor(self, sensor_id): """ return sensor representation """ return self._sensors[sensor_id] def turn_switch_on(self, switch_id): """ Turn switch off. """ if self._check_request("device/turnOn", id=switch_id): from tellive.live import const self.get_switch(switch_id)["state"] = const.TELLSTICK_TURNON def turn_switch_off(self, switch_id): """ Turn switch on. """ if self._check_request("device/turnOff", id=switch_id): from tellive.live import const self.get_switch(switch_id)["state"] = const.TELLSTICK_TURNOFF