Skip to content

Commit f76631a

Browse files
author
Ted Roberts
committed
Default raw command body ids when omitted
1 parent eef97fa commit f76631a

2 files changed

Lines changed: 64 additions & 57 deletions

File tree

custom_components/novastar_h/__init__.py

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -142,68 +142,75 @@ def resolve_coordinator_by_host(
142142
"Multiple Novastar devices configured; specify host for this service call",
143143
)
144144

145-
# Register send_raw_command service if not already registered
146-
# Check is done at call time via options/data
147-
if not hass.services.has_service(DOMAIN, SERVICE_SEND_RAW_COMMAND):
148-
async def async_send_raw_command(call: ServiceCall) -> None:
149-
"""Handle send_raw_command service call."""
150-
host = call.data.get(CONF_HOST)
151-
endpoint = call.data[ATTR_ENDPOINT]
152-
body = call.data[ATTR_BODY]
153-
154-
coordinator_found, resolved_host, error_message = resolve_coordinator_by_host(
155-
host
156-
)
157-
158-
if coordinator_found is None:
159-
_LOGGER.error(error_message)
160-
return
145+
# Always refresh send_raw_command registration so schema changes apply.
146+
if hass.services.has_service(DOMAIN, SERVICE_SEND_RAW_COMMAND):
147+
hass.services.async_remove(DOMAIN, SERVICE_SEND_RAW_COMMAND)
148+
149+
async def async_send_raw_command(call: ServiceCall) -> None:
150+
"""Handle send_raw_command service call."""
151+
host = call.data.get(CONF_HOST)
152+
if isinstance(host, str):
153+
host = host.strip() or None
154+
155+
endpoint = call.data[ATTR_ENDPOINT]
156+
body = call.data[ATTR_BODY]
157+
effective_body = dict(body)
158+
effective_body.setdefault("deviceId", 0)
159+
effective_body.setdefault("screenId", 0)
160+
161+
coordinator_found, resolved_host, error_message = resolve_coordinator_by_host(
162+
host
163+
)
161164

162-
client_found = None
163-
raw_enabled = False
164-
for eid, data in hass.data[DOMAIN].items():
165-
if not isinstance(data, dict) or "client" not in data:
166-
continue
167-
client = data["client"]
168-
if client.host != resolved_host:
169-
continue
170-
client_found = client
171-
172-
config_entry = hass.config_entries.async_get_entry(eid)
173-
if config_entry:
174-
raw_enabled = config_entry.options.get(
175-
CONF_ALLOW_RAW_COMMANDS,
176-
config_entry.data.get(
177-
CONF_ALLOW_RAW_COMMANDS, DEFAULT_ALLOW_RAW_COMMANDS
178-
),
179-
)
180-
break
165+
if coordinator_found is None:
166+
_LOGGER.error(error_message)
167+
return
181168

182-
if not client_found:
183-
_LOGGER.error(
184-
"No Novastar device found at %s", resolved_host
185-
)
186-
return
169+
client_found = None
170+
raw_enabled = False
171+
for eid, data in hass.data[DOMAIN].items():
172+
if not isinstance(data, dict) or "client" not in data:
173+
continue
174+
client = data["client"]
175+
if client.host != resolved_host:
176+
continue
177+
client_found = client
187178

188-
if not raw_enabled:
189-
_LOGGER.error(
190-
"Raw commands are not enabled for Novastar device at %s",
191-
resolved_host,
179+
config_entry = hass.config_entries.async_get_entry(eid)
180+
if config_entry:
181+
raw_enabled = config_entry.options.get(
182+
CONF_ALLOW_RAW_COMMANDS,
183+
config_entry.data.get(
184+
CONF_ALLOW_RAW_COMMANDS, DEFAULT_ALLOW_RAW_COMMANDS
185+
),
192186
)
193-
return
187+
break
194188

195-
result = await client_found.async_send_raw_command(endpoint, body)
196-
if result is None:
197-
_LOGGER.warning("Raw command to %s failed", endpoint)
198-
else:
199-
_LOGGER.debug("Raw command result: %s", result)
189+
if not client_found:
190+
_LOGGER.error(
191+
"No Novastar device found at %s", resolved_host
192+
)
193+
return
200194

201-
hass.services.async_register(
202-
DOMAIN,
203-
SERVICE_SEND_RAW_COMMAND,
204-
async_send_raw_command,
205-
schema=SERVICE_SEND_RAW_COMMAND_SCHEMA,
206-
)
195+
if not raw_enabled:
196+
_LOGGER.error(
197+
"Raw commands are not enabled for Novastar device at %s",
198+
resolved_host,
199+
)
200+
return
201+
202+
result = await client_found.async_send_raw_command(endpoint, effective_body)
203+
if result is None:
204+
_LOGGER.warning("Raw command to %s failed", endpoint)
205+
else:
206+
_LOGGER.debug("Raw command result: %s", result)
207+
208+
hass.services.async_register(
209+
DOMAIN,
210+
SERVICE_SEND_RAW_COMMAND,
211+
async_send_raw_command,
212+
schema=SERVICE_SEND_RAW_COMMAND_SCHEMA,
213+
)
207214

208215
if not hass.services.has_service(DOMAIN, SERVICE_SET_LAYER_SOURCE):
209216
async def async_set_layer_source(call: ServiceCall) -> None:

custom_components/novastar_h/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
}
2222
],
2323
"zeroconf": ["_novastar._tcp.local."],
24-
"version": "0.2.60"
24+
"version": "0.2.61"
2525
}

0 commit comments

Comments
 (0)