This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
services.py
166 lines (134 loc) · 4.85 KB
/
services.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""Services for nibe uplink."""
from __future__ import annotations
import json
import logging
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.const import ATTR_NAME, ATTR_TEMPERATURE
from homeassistant.helpers.event import async_call_later
from nibeuplink.uplink import Uplink
from .const import (
ATTR_TARGET_TEMPERATURE,
ATTR_VALVE_POSITION,
DATA_NIBE_ENTRIES,
DOMAIN,
SERVICE_GET_PARAMETER,
SERVICE_SET_PARAMETER,
SERVICE_SET_SMARTHOME_MODE,
SERVICE_SET_THERMOSTAT,
)
_LOGGER = logging.getLogger(__name__)
def async_track_delta_time(hass, delta, callable):
"""
Run callable cyclicly.
Similar to async_track_time_interval, but where we
instead guarantee delta between calls
"""
remove = None
skipped = False
def skip():
nonlocal skipped
skipped = True
async def fun(now):
nonlocal remove
remove = skip
try:
await callable()
finally:
if not skipped:
remove = async_call_later(hass, delta, fun)
def remover():
remove()
remove = async_call_later(hass, delta, fun)
return remover
async def async_register_services(hass):
"""Register public services."""
from nibeuplink import SMARTHOME_MODES, SetThermostatModel
def _find_uplink(system: int) -> Uplink:
from . import NibeData
entries: dict[str, NibeData] = hass.data[DATA_NIBE_ENTRIES]
for data in entries.values():
if system in data.systems:
return data.uplink
else:
raise Exception(f"Can't find uplink with system identifier {system}")
async def set_smarthome_mode(call):
"""Set smarthome mode."""
uplink = _find_uplink(call.data["system"])
await uplink.put_smarthome_mode(call.data["system"], call.data["mode"])
async def set_parameter(call):
uplink = _find_uplink(call.data["system"])
await uplink.put_parameter(
call.data["system"], call.data["parameter"], call.data["value"]
)
async def get_parameter(call):
uplink = _find_uplink(call.data["system"])
data = await uplink.get_parameter(call.data["system"], call.data["parameter"])
hass.components.persistent_notification.async_create(
json.dumps(data, indent=1), "Nibe get parameter result"
)
async def set_thermostat(call):
uplink = _find_uplink(call.data["system"])
def scaled(value, multi=10):
if value is None:
return None
else:
return round(value * multi)
data = SetThermostatModel(
externalId=call.data["id"],
name=call.data[ATTR_NAME],
actualTemp=scaled(call.data[ATTR_TEMPERATURE]),
targetTemp=scaled(call.data[ATTR_TARGET_TEMPERATURE]),
valvePosition=call.data[ATTR_VALVE_POSITION],
climateSystems=call.data["systems"],
)
_LOGGER.debug(f"Publish thermostat {data}")
await uplink.post_smarthome_thermostats(call.data["system"], data)
SERVICE_SET_SMARTHOME_MODE_SCHEMA = vol.Schema(
{
vol.Required("system"): cv.positive_int,
vol.Required("mode"): vol.In(SMARTHOME_MODES.values()),
}
)
SERVICE_SET_PARAMETER_SCHEMA = vol.Schema(
{
vol.Required("system"): cv.positive_int,
vol.Required("parameter"): cv.string,
vol.Required("value"): cv.string,
}
)
SERVICE_GET_PARAMETER_SCHEMA = vol.Schema(
{vol.Required("system"): cv.positive_int, vol.Required("parameter"): cv.string}
)
SERVICE_SET_THERMOSTAT_SCHEMA = vol.Schema(
{
vol.Required("system"): cv.positive_int,
vol.Required("id"): cv.positive_int,
vol.Required(ATTR_NAME): cv.string,
vol.Required("systems"): cv.ensure_list,
vol.Optional(ATTR_TEMPERATURE, default=None): vol.Any(
None, vol.Coerce(float)
),
vol.Optional(ATTR_TARGET_TEMPERATURE, default=None): vol.Any(
None, vol.Coerce(float)
),
vol.Optional(ATTR_VALVE_POSITION, default=None): vol.Any(
None, vol.All(vol.Coerce(int), vol.Range(min=0, max=100))
),
}
)
hass.services.async_register(
DOMAIN,
SERVICE_SET_SMARTHOME_MODE,
set_smarthome_mode,
SERVICE_SET_SMARTHOME_MODE_SCHEMA,
)
hass.services.async_register(
DOMAIN, SERVICE_SET_PARAMETER, set_parameter, SERVICE_SET_PARAMETER_SCHEMA
)
hass.services.async_register(
DOMAIN, SERVICE_GET_PARAMETER, get_parameter, SERVICE_GET_PARAMETER_SCHEMA
)
hass.services.async_register(
DOMAIN, SERVICE_SET_THERMOSTAT, set_thermostat, SERVICE_SET_THERMOSTAT_SCHEMA
)