Skip to content

Commit eef97fa

Browse files
author
Ted Roberts
committed
Make send_raw_command host optional for single device
1 parent 6c309a1 commit eef97fa

2 files changed

Lines changed: 36 additions & 19 deletions

File tree

custom_components/novastar_h/__init__.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
SERVICE_SEND_RAW_COMMAND_SCHEMA = vol.Schema(
4848
{
49-
vol.Required(CONF_HOST): cv.string,
49+
vol.Optional(CONF_HOST): cv.string,
5050
vol.Required(ATTR_ENDPOINT): cv.string,
5151
vol.Required(ATTR_BODY): dict,
5252
}
@@ -147,31 +147,48 @@ def resolve_coordinator_by_host(
147147
if not hass.services.has_service(DOMAIN, SERVICE_SEND_RAW_COMMAND):
148148
async def async_send_raw_command(call: ServiceCall) -> None:
149149
"""Handle send_raw_command service call."""
150-
host = call.data[CONF_HOST]
150+
host = call.data.get(CONF_HOST)
151151
endpoint = call.data[ATTR_ENDPOINT]
152152
body = call.data[ATTR_BODY]
153153

154-
# Find the client for the specified host
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
161+
155162
client_found = None
163+
raw_enabled = False
156164
for eid, data in hass.data[DOMAIN].items():
157-
if isinstance(data, dict) and "client" in data:
158-
if data["client"].host == host:
159-
# Check if this entry allows raw commands (options first, then data)
160-
config_entry = hass.config_entries.async_get_entry(eid)
161-
if config_entry:
162-
allow_raw = config_entry.options.get(
163-
CONF_ALLOW_RAW_COMMANDS,
164-
config_entry.data.get(
165-
CONF_ALLOW_RAW_COMMANDS, DEFAULT_ALLOW_RAW_COMMANDS
166-
),
167-
)
168-
if allow_raw:
169-
client_found = data["client"]
170-
break
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
171181

172182
if not client_found:
173183
_LOGGER.error(
174-
"No Novastar device found at %s with raw commands enabled", host
184+
"No Novastar device found at %s", resolved_host
185+
)
186+
return
187+
188+
if not raw_enabled:
189+
_LOGGER.error(
190+
"Raw commands are not enabled for Novastar device at %s",
191+
resolved_host,
175192
)
176193
return
177194

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.59"
24+
"version": "0.2.60"
2525
}

0 commit comments

Comments
 (0)