|
46 | 46 |
|
47 | 47 | SERVICE_SEND_RAW_COMMAND_SCHEMA = vol.Schema( |
48 | 48 | { |
49 | | - vol.Required(CONF_HOST): cv.string, |
| 49 | + vol.Optional(CONF_HOST): cv.string, |
50 | 50 | vol.Required(ATTR_ENDPOINT): cv.string, |
51 | 51 | vol.Required(ATTR_BODY): dict, |
52 | 52 | } |
@@ -147,31 +147,48 @@ def resolve_coordinator_by_host( |
147 | 147 | if not hass.services.has_service(DOMAIN, SERVICE_SEND_RAW_COMMAND): |
148 | 148 | async def async_send_raw_command(call: ServiceCall) -> None: |
149 | 149 | """Handle send_raw_command service call.""" |
150 | | - host = call.data[CONF_HOST] |
| 150 | + host = call.data.get(CONF_HOST) |
151 | 151 | endpoint = call.data[ATTR_ENDPOINT] |
152 | 152 | body = call.data[ATTR_BODY] |
153 | 153 |
|
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 | + |
155 | 162 | client_found = None |
| 163 | + raw_enabled = False |
156 | 164 | 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 |
171 | 181 |
|
172 | 182 | if not client_found: |
173 | 183 | _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, |
175 | 192 | ) |
176 | 193 | return |
177 | 194 |
|
|
0 commit comments