def message_received(topic, payload, qos): """ A new MQTT message has been received. """ if value_template is not None: payload = template.render_with_possible_json_value( hass, value_template, payload) self._state = payload self.update_ha_state()
def setup_platform(hass, config, add_devices, discovery_info=None): """ Setup the Dweet sensor. """ import dweepy device = config.get('device') value_template = config.get(CONF_VALUE_TEMPLATE) if None in (device, value_template): _LOGGER.error('Not all required config keys present: %s', ', '.join(CONF_DEVICE, CONF_VALUE_TEMPLATE)) return False try: content = json.dumps(dweepy.get_latest_dweet_for(device)[0]['content']) except dweepy.DweepyError: _LOGGER.error("Device/thing '%s' could not be found", device) return False if template.render_with_possible_json_value(hass, value_template, content) is '': _LOGGER.error("'%s' was not found", value_template) return False dweet = DweetData(device) add_devices([DweetSensor(hass, dweet, config.get('name', DEFAULT_NAME), value_template, config.get('unit_of_measurement'))])
def update(self): """ Update device state. """ if self._command_state: payload = str(self._query_state()) if self._value_template: payload = template.render_with_possible_json_value( self._hass, self._value_template, payload) self._state = (payload.lower() == "true")
def state(self): """ Returns the state. """ if self.dweet.data is None: return STATE_UNKNOWN else: values = json.dumps(self.dweet.data[0]['content']) value = template.render_with_possible_json_value( self.hass, self._value_template, values) return value
def is_on(self): """Return if the binary sensor is on.""" if self.rest.data is None: return False if self._value_template is not None: self.rest.data = template.render_with_possible_json_value( self._hass, self._value_template, self.rest.data, False) return bool(int(self.rest.data))
def update(self): """ Gets the latest data and updates the state. """ self.data.update() value = self.data.value if self._value_template is not None: self._state = template.render_with_possible_json_value(self._hass, self._value_template, value, "N/A") else: self._state = value
def message_received(topic, payload, qos): """ A new MQTT message has been received. """ if value_template is not None: payload = template.render_with_possible_json_value( hass, value_template, payload) if payload.isnumeric() and 0 <= int(payload) <= 100: self._state = int(payload) self.update_ha_state() else: _LOGGER.warning( "Payload is expected to be an integer between 0 and 100")
def message_received(topic, payload, qos): """ A new MQTT message has been received. """ if value_template is not None: payload = template.render_with_possible_json_value( hass, value_template, payload) if payload == self._payload_on: self._state = True self.update_ha_state() elif payload == self._payload_off: self._state = False self.update_ha_state()
def update(self): """ Gets the latest data and updates the state. """ self.data.update() value = self.data.value if self._value_template is not None: value = template.render_with_possible_json_value( self._hass, self._value_template, value, False) if value == self._payload_on: self._state = True elif value == self._payload_off: self._state = False
def update(self): """ Gets the latest data from REST API and updates the state. """ self.rest.update() value = self.rest.data if 'error' in value: self._state = value['error'] else: if self._value_template is not None: value = template.render_with_possible_json_value( self._hass, self._value_template, value, 'N/A') self._state = value
def update(self): """ Gets the latest data from REST API and updates the state. """ self.rest.update() value = self.rest.data if value is None: value = STATE_UNKNOWN elif self._value_template is not None: value = template.render_with_possible_json_value( self._hass, self._value_template, value, STATE_UNKNOWN) self._state = value
def test_render_with_possible_json_value_with_invalid_json(self): self.assertEqual( '', template.render_with_possible_json_value(self.hass, '{{ value_json }}', '{ I AM NOT JSON }'))
def test_render_with_possible_json_value_with_template_error(self): self.assertEqual( 'hello', template.render_with_possible_json_value(self.hass, '{{ value_json', 'hello'))
def test_render_with_possible_json_value_with_valid_json(self): self.assertEqual( 'world', template.render_with_possible_json_value(self.hass, '{{ value_json.hello }}', '{"hello": "world"}'))
def test_render_with_possible_json_value_with_valid_json(self): self.assertEqual( 'world', template.render_with_possible_json_value( self.hass, '{{ value_json.hello }}', '{"hello": "world"}'))
def test_render_with_possible_json_value_with_invalid_json(self): self.assertEqual( '', template.render_with_possible_json_value( self.hass, '{{ value_json }}', '{ I AM NOT JSON }'))
def test_render_with_possible_json_value_with_template_error_error_value(self): self.assertEqual( '-', template.render_with_possible_json_value( self.hass, '{{ value_json', 'hello', '-'))