Esempio n. 1
0
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'))])
Esempio n. 2
0
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'))])
Esempio n. 3
0
 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"
Esempio n. 4
0
 def test_render_with_possible_json_value_with_template_error_value(self):
     """."""
     self.assertEqual(
         '-',
         template.render_with_possible_json_value(self.hass,
                                                  '{{ value_json', 'hello',
                                                  '-'))
Esempio n. 5
0
 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()
Esempio n. 6
0
 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"}'))
Esempio n. 7
0
 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 }'))
Esempio n. 8
0
 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()
Esempio n. 9
0
 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 = int(payload)
Esempio n. 10
0
 def state(self):
     """Return 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
Esempio n. 11
0
    def is_on(self):
        """Return true 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))
Esempio n. 12
0
 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
Esempio n. 13
0
    def is_on(self):
        """Return true 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))
Esempio n. 14
0
    def update(self):
        """Get 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
Esempio n. 15
0
 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")
Esempio n. 16
0
 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")
Esempio n. 17
0
 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()
Esempio n. 18
0
 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()
Esempio n. 19
0
    def update(self):
        """Get the latest data from REST API and update 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
Esempio n. 20
0
    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
Esempio n. 21
0
    def update(self):
        """Get the latest data from REST API and update 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
Esempio n. 22
0
    def is_on(self):
        """Return true if the binary sensor is on."""
        if self.rest.data is None:
            return False

        if self._value_template is not None:
            response = template.render_with_possible_json_value(
                self._hass, self._value_template, self.rest.data, False)

        try:
            return bool(int(response))
        except ValueError:
            return {"true": True, "on": True, "open": True,
                    "yes": True}.get(response.lower(), False)
Esempio n. 23
0
    def is_on(self):
        """Return true if the binary sensor is on."""
        if self.rest.data is None:
            return False

        if self._value_template is not None:
            response = template.render_with_possible_json_value(
                self._hass, self._value_template, self.rest.data, False)

        try:
            return bool(int(response))
        except ValueError:
            return {"true": True, "on": True, "open": True,
                    "yes": True}.get(response.lower(), False)
Esempio n. 24
0
 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._state_open:
         self._state = False
         self.update_ha_state()
     elif payload == self._state_closed:
         self._state = True
         self.update_ha_state()
     elif payload.isnumeric() and 0 <= int(payload) <= 100:
         self._state = int(payload)
         self._position = int(payload)
         self.update_ha_state()
     else:
         _LOGGER.warning(
             "Payload is not True or False or"
             " integer(0-100) %s", payload)
Esempio n. 25
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the Dweet sensor."""
    import dweepy

    name = config.get(CONF_NAME)
    device = config.get(CONF_DEVICE)
    value_template = config.get(CONF_VALUE_TEMPLATE)
    unit = config.get(CONF_UNIT_OF_MEASUREMENT)

    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, name, value_template, unit)])
Esempio n. 26
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the Dweet sensor."""
    import dweepy

    name = config.get(CONF_NAME)
    device = config.get(CONF_DEVICE)
    value_template = config.get(CONF_VALUE_TEMPLATE)
    unit = config.get(CONF_UNIT_OF_MEASUREMENT)

    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, name, value_template, unit)])
Esempio n. 27
0
 def test_render_with_possible_json_value_with_template_error_value(self):
     """."""
     self.assertEqual("-", template.render_with_possible_json_value(self.hass, "{{ value_json", "hello", "-"))
Esempio n. 28
0
 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 }")
     )
Esempio n. 29
0
 def test_render_with_possible_json_value_with_template_error_value(self):
     """."""
     self.assertEqual(
         '-',
         template.render_with_possible_json_value(
             self.hass, '{{ value_json', 'hello', '-'))
Esempio n. 30
0
 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 }'))
Esempio n. 31
0
 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"}'))