1 """Support for sending data to Emoncms."""
3 from datetime
import timedelta
4 from http
import HTTPStatus
8 import voluptuous
as vol
25 _LOGGER = logging.getLogger(__name__)
27 DOMAIN =
"emoncms_history"
28 CONF_INPUTNODE =
"inputnode"
30 CONFIG_SCHEMA = vol.Schema(
34 vol.Required(CONF_API_KEY): cv.string,
35 vol.Required(CONF_URL): cv.string,
36 vol.Required(CONF_INPUTNODE): cv.positive_int,
37 vol.Required(CONF_WHITELIST): cv.entity_ids,
38 vol.Optional(CONF_SCAN_INTERVAL, default=30): cv.positive_int,
42 extra=vol.ALLOW_EXTRA,
46 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
47 """Set up the Emoncms history component."""
49 whitelist = conf.get(CONF_WHITELIST)
51 def send_data(url, apikey, node, payload):
52 """Send payload data to Emoncms."""
54 fullurl = f
"{url}/input/post.json"
55 data = {
"apikey": apikey,
"data": payload}
56 parameters = {
"node": node}
58 fullurl, params=parameters, data=data, allow_redirects=
True, timeout=5
61 except requests.exceptions.RequestException:
62 _LOGGER.error(
"Error saving data '%s' to '%s'", payload, fullurl)
65 if req.status_code != HTTPStatus.OK:
67 "Error saving data %s to %s (http status code = %d)",
73 def update_emoncms(time):
74 """Send whitelisted entities states regularly to Emoncms."""
77 for entity_id
in whitelist:
78 state = hass.states.get(entity_id)
80 if state
is None or state.state
in (STATE_UNKNOWN,
"", STATE_UNAVAILABLE):
84 payload_dict[entity_id] = state_helper.state_as_number(state)
89 payload =
",".join(f
"{key}:{val}" for key, val
in payload_dict.items())
93 conf.get(CONF_API_KEY),
94 str(conf.get(CONF_INPUTNODE)),
99 hass, update_emoncms, time +
timedelta(seconds=conf.get(CONF_SCAN_INTERVAL))
102 update_emoncms(dt_util.utcnow())
bool setup(HomeAssistant hass, ConfigType config)