1 """Property update methods and schemas."""
5 from pyinsteon
import devices
6 from pyinsteon.config
import (
12 from pyinsteon.constants
import (
19 from pyinsteon.device_types.device_base
import Device
20 import voluptuous
as vol
21 import voluptuous_serialize
30 INSTEON_DEVICE_NOT_FOUND,
35 from .device
import notify_device_not_found
37 SHOW_ADVANCED =
"show_advanced"
38 RAMP_RATE_SECONDS =
list(dict.fromkeys(RAMP_RATES_SEC))
39 RAMP_RATE_SECONDS.sort()
40 RAMP_RATE_LIST = [
str(seconds)
for seconds
in RAMP_RATE_SECONDS]
41 TOGGLE_MODES = [
str(ToggleMode(v)).lower()
for v
in list(ToggleMode)]
42 RELAY_MODES = [
str(RelayMode(v)).lower()
for v
in list(RelayMode)]
46 return voluptuous_serialize.convert(vol.Schema({vol.Required(name): bool}))[0]
50 return voluptuous_serialize.convert(vol.Schema({vol.Required(name): cv.byte}))[0]
54 return voluptuous_serialize.convert(vol.Schema({vol.Required(name): float}))[0]
58 return voluptuous_serialize.convert(
59 vol.Schema({vol.Required(name): vol.In(values)}),
60 custom_serializer=cv.custom_serializer,
65 return voluptuous_serialize.convert(
66 vol.Schema({vol.Optional(name): cv.multi_select(values)}),
67 custom_serializer=cv.custom_serializer,
72 """Return a constant value schema."""
73 return voluptuous_serialize.convert(vol.Schema({vol.Required(name): value}))[0]
77 """Return the correct schema type."""
80 if name == RAMP_RATE_IN_SEC:
82 if name == RADIO_BUTTON_GROUPS:
83 button_list = {
str(group): groups[group].name
for group
in groups}
85 if name == LOAD_BUTTON:
86 button_list = {group: groups[group].name
for group
in groups}
88 if prop.value_type
is bool:
90 if prop.value_type
is int:
92 if prop.value_type
is float:
94 if prop.value_type == ToggleMode:
96 if prop.value_type == RelayMode:
102 """Get the properties of an Insteon device and return the records and schema."""
107 for name, prop
in device.configuration.items():
108 if prop.is_read_only
and not show_advanced:
111 prop_schema =
get_schema(prop, name, device.groups)
112 if prop_schema
is None:
114 schema[name] = prop_schema
118 for name, prop
in device.operating_flags.items():
119 if prop.property_type != PropertyType.ADVANCED:
121 prop_schema =
get_schema(prop, name, device.groups)
122 if prop_schema
is not None:
123 schema[name] = prop_schema
125 for name, prop
in device.properties.items():
126 if prop.property_type != PropertyType.ADVANCED:
128 prop_schema =
get_schema(prop, name, device.groups)
129 if prop_schema
is not None:
130 schema[name] = prop_schema
133 return properties, schema
137 """Return a property data row."""
138 value = get_usable_value(prop)
139 modified = value == prop.new_value
140 if prop.value_type
in [ToggleMode, RelayMode]
or prop.name == RAMP_RATE_IN_SEC:
141 value =
str(value).lower()
142 return {
"name": prop.name,
"value": value,
"modified": modified}
146 """Update the value of a device property."""
147 prop = device.configuration[prop_name]
148 if prop.value_type == ToggleMode:
149 toggle_mode = getattr(ToggleMode, value.upper())
150 prop.new_value = toggle_mode
151 elif prop.value_type == RelayMode:
152 relay_mode = getattr(RelayMode, value.upper())
153 prop.new_value = relay_mode
155 prop.new_value = value
158 @websocket_api.websocket_command(
{
vol.Required(TYPE):
"insteon/properties/get",
159 vol.Required(DEVICE_ADDRESS): str,
160 vol.Required(SHOW_ADVANCED): bool,
163 @websocket_api.require_admin
164 @websocket_api.async_response
170 """Add the default All-Link Database records for an Insteon device."""
171 if not (device := devices[msg[DEVICE_ADDRESS]]):
177 connection.send_result(msg[ID], {
"properties": properties,
"schema": schema})
180 @websocket_api.websocket_command(
{
vol.Required(TYPE):
"insteon/properties/change",
181 vol.Required(DEVICE_ADDRESS): str,
182 vol.Required(PROPERTY_NAME): str,
183 vol.Required(PROPERTY_VALUE): vol.Any(list, int, float, bool, str),
186 @websocket_api.require_admin
187 @websocket_api.async_response
193 """Add the default All-Link Database records for an Insteon device."""
194 if not (device := devices[msg[DEVICE_ADDRESS]]):
199 connection.send_result(msg[ID])
202 @websocket_api.websocket_command(
{
vol.Required(TYPE):
"insteon/properties/write",
203 vol.Required(DEVICE_ADDRESS): str,
206 @websocket_api.require_admin
207 @websocket_api.async_response
213 """Add the default All-Link Database records for an Insteon device."""
214 if not (device := devices[msg[DEVICE_ADDRESS]]):
218 result = await device.async_write_config()
219 await devices.async_save(workdir=hass.config.config_dir)
220 if result
not in [ResponseStatus.SUCCESS, ResponseStatus.RUN_ON_WAKE]:
221 connection.send_message(
222 websocket_api.error_message(
223 msg[ID],
"write_failed",
"properties not written to device"
227 connection.send_result(msg[ID])
230 @websocket_api.websocket_command(
{
vol.Required(TYPE):
"insteon/properties/load",
231 vol.Required(DEVICE_ADDRESS): str,
234 @websocket_api.require_admin
235 @websocket_api.async_response
241 """Add the default All-Link Database records for an Insteon device."""
242 if not (device := devices[msg[DEVICE_ADDRESS]]):
246 result = await device.async_read_config(read_aldb=
False)
247 await devices.async_save(workdir=hass.config.config_dir)
249 if result
not in [ResponseStatus.SUCCESS, ResponseStatus.RUN_ON_WAKE]:
250 connection.send_message(
251 websocket_api.error_message(
252 msg[ID],
"load_failed",
"properties not loaded from device"
256 connection.send_result(msg[ID])
259 @websocket_api.websocket_command(
{
vol.Required(TYPE):
"insteon/properties/reset",
260 vol.Required(DEVICE_ADDRESS): str,
263 @websocket_api.require_admin
264 @websocket_api.async_response
270 """Add the default All-Link Database records for an Insteon device."""
271 if not (device := devices[msg[DEVICE_ADDRESS]]):
275 for prop
in device.operating_flags:
276 device.operating_flags[prop].new_value =
None
277 for prop
in device.properties:
278 device.properties[prop].new_value =
None
279 connection.send_result(msg[ID])
280
def notify_device_not_found(connection, msg, text)
def _list_schema(name, values)
None websocket_reset_properties(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
def _multi_select_schema(name, values)
None websocket_write_properties(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
None websocket_get_properties(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
None websocket_load_properties(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
def get_properties(Device device, show_advanced=False)
None websocket_change_properties_record(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
def update_property(device, prop_name, value)
def property_to_dict(prop)
def get_schema(prop, name, groups)
def _read_only_schema(name, value)