forked from uvejota/homeassistant-edata
/
websockets.py
103 lines (88 loc) · 3.43 KB
/
websockets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Websockets related definitions"""
import logging
import voluptuous as vol
from homeassistant.components import websocket_api
from homeassistant.core import callback
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
@callback
def websocket_get_daily_data(hass, connection, msg):
"""Publish daily consumptions list data."""
try:
data = hass.data[DOMAIN][msg["scups"].upper()].get("ws_consumptions_day", [])
# served data is filtered so only last 'records' records are represented
connection.send_result(msg["id"], data[-msg.get("records", 30) :])
except KeyError as _:
_LOGGER.error(
"The provided scups parameter is not correct: %s", msg["scups"].upper()
)
except Exception as _:
_LOGGER.exception("Unhandled exception when processing websockets: %s", _)
connection.send_result(msg["id"], [])
@callback
def websocket_get_monthly_data(hass, connection, msg):
"""Publish monthly consumptions list data."""
try:
connection.send_result(
msg["id"],
hass.data[DOMAIN][msg["scups"].upper()].get("ws_consumptions_month", []),
)
except KeyError as _:
_LOGGER.error(
"The provided scups parameter is not correct: %s", msg["scups"].upper()
)
except Exception as _:
_LOGGER.exception("Unhandled exception when processing websockets: %s", _)
connection.send_result(msg["id"], [])
@callback
def websocket_get_maximeter(hass, connection, msg):
"""Publish maximeter list data."""
try:
data = hass.data[DOMAIN][msg["scups"].upper()].get("ws_maximeter", [])
if "tariff" in msg:
data = [x for x in data if x[f"value_p{msg['tariff']}_kW"] > 0]
connection.send_result(msg["id"], data)
except KeyError as _:
_LOGGER.error(
"The provided scups parameter is not correct: %s", msg["scups"].upper()
)
except Exception as _:
_LOGGER.exception("Unhandled exception when processing websockets: %s", _)
connection.send_result(msg["id"], [])
def async_register_websockets(hass):
"""Register websockets into HA API"""
# for daily consumptions
hass.components.websocket_api.async_register_command(
f"{DOMAIN}/consumptions/daily",
websocket_get_daily_data,
websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
{
vol.Required("type"): f"{DOMAIN}/consumptions/daily",
vol.Required("scups"): str,
vol.Optional("records"): int,
}
),
)
# for monthly consumptions
hass.components.websocket_api.async_register_command(
f"{DOMAIN}/consumptions/monthly",
websocket_get_monthly_data,
websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
{
vol.Required("type"): f"{DOMAIN}/consumptions/monthly",
vol.Required("scups"): str,
}
),
)
# for maximeter
hass.components.websocket_api.async_register_command(
f"{DOMAIN}/maximeter",
websocket_get_maximeter,
websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
{
vol.Required("type"): f"{DOMAIN}/maximeter",
vol.Required("scups"): str,
vol.Optional("tariff"): int,
}
),
)