1 """Support for showing values from Dweet.io."""
3 from __future__
import annotations
5 from datetime
import timedelta
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
19 CONF_UNIT_OF_MEASUREMENT,
27 _LOGGER = logging.getLogger(__name__)
29 DEFAULT_NAME =
"Dweet.io Sensor"
33 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
35 vol.Required(CONF_DEVICE): cv.string,
36 vol.Required(CONF_VALUE_TEMPLATE): cv.template,
37 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
38 vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
46 add_entities: AddEntitiesCallback,
47 discovery_info: DiscoveryInfoType |
None =
None,
49 """Set up the Dweet sensor."""
50 name = config.get(CONF_NAME)
51 device = config.get(CONF_DEVICE)
52 value_template = config.get(CONF_VALUE_TEMPLATE)
53 unit = config.get(CONF_UNIT_OF_MEASUREMENT)
56 content = json.dumps(dweepy.get_latest_dweet_for(device)[0][
"content"])
57 except dweepy.DweepyError:
58 _LOGGER.error(
"Device/thing %s could not be found", device)
61 if value_template
and value_template.render_with_possible_json_value(content) ==
"":
62 _LOGGER.error(
"%s was not found", value_template)
71 """Representation of a Dweet sensor."""
73 def __init__(self, hass, dweet, name, value_template, unit_of_measurement):
74 """Initialize the sensor."""
84 """Return the name of the sensor."""
85 return self.
_name_name
89 """Return the unit the value is expressed in."""
94 """Return the state."""
98 """Get the latest data from REST API."""
101 if self.
dweetdweet.data
is None:
104 values = json.dumps(self.
dweetdweet.data[0][
"content"])
111 """The class for handling the data retrieval."""
114 """Initialize the sensor."""
119 """Get the latest data from Dweet.io."""
121 self.
datadata = dweepy.get_latest_dweet_for(self.
_device_device)
122 except dweepy.DweepyError:
123 _LOGGER.warning(
"Device %s doesn't contain any data", self.
_device_device)
def __init__(self, device)
def native_unit_of_measurement(self)
def __init__(self, hass, dweet, name, value_template, unit_of_measurement)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def add_entities(account, async_add_entities, tracked)