mirror of
https://github.com/esphome/esphome.git
synced 2026-05-10 05:37:55 +08:00
[dashboard] Add --no-states support to logs WebSocket handler (#15993)
This commit is contained in:
@@ -437,7 +437,11 @@ class EsphomePortCommandWebSocket(EsphomeCommandWebSocket):
|
||||
class EsphomeLogsHandler(EsphomePortCommandWebSocket):
|
||||
async def build_command(self, json_message: dict[str, Any]) -> list[str]:
|
||||
"""Build the command to run."""
|
||||
return await self.build_device_command(["logs"], json_message)
|
||||
cmd = await self.build_device_command(["logs"], json_message)
|
||||
if json_message.get("no_states"):
|
||||
cmd.append("--no-states")
|
||||
_LOGGER.debug("Built command: %s", cmd)
|
||||
return cmd
|
||||
|
||||
|
||||
class EsphomeRenameHandler(EsphomeCommandWebSocket):
|
||||
|
||||
@@ -1744,6 +1744,64 @@ def test_proc_on_exit_skips_when_already_closed() -> None:
|
||||
handler.close.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_esphome_logs_handler_appends_no_states_when_set() -> None:
|
||||
"""Test --no-states is appended when no_states is truthy in the message."""
|
||||
handler = Mock(spec=web_server.EsphomeLogsHandler)
|
||||
handler.build_device_command = AsyncMock(
|
||||
return_value=["esphome", "logs", "device.yaml", "--device", "OTA"]
|
||||
)
|
||||
|
||||
json_message = {
|
||||
"configuration": "device.yaml",
|
||||
"port": "OTA",
|
||||
"no_states": True,
|
||||
}
|
||||
cmd = await web_server.EsphomeLogsHandler.build_command(handler, json_message)
|
||||
|
||||
assert cmd == [
|
||||
"esphome",
|
||||
"logs",
|
||||
"device.yaml",
|
||||
"--device",
|
||||
"OTA",
|
||||
"--no-states",
|
||||
]
|
||||
handler.build_device_command.assert_awaited_once_with(["logs"], json_message)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_esphome_logs_handler_omits_no_states_when_missing() -> None:
|
||||
"""Test --no-states is not added when no_states is absent from the message."""
|
||||
handler = Mock(spec=web_server.EsphomeLogsHandler)
|
||||
handler.build_device_command = AsyncMock(
|
||||
return_value=["esphome", "logs", "device.yaml", "--device", "OTA"]
|
||||
)
|
||||
|
||||
cmd = await web_server.EsphomeLogsHandler.build_command(
|
||||
handler, {"configuration": "device.yaml", "port": "OTA"}
|
||||
)
|
||||
|
||||
assert "--no-states" not in cmd
|
||||
assert cmd == ["esphome", "logs", "device.yaml", "--device", "OTA"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_esphome_logs_handler_omits_no_states_when_false() -> None:
|
||||
"""Test --no-states is not added when no_states is explicitly False."""
|
||||
handler = Mock(spec=web_server.EsphomeLogsHandler)
|
||||
handler.build_device_command = AsyncMock(
|
||||
return_value=["esphome", "logs", "device.yaml", "--device", "OTA"]
|
||||
)
|
||||
|
||||
cmd = await web_server.EsphomeLogsHandler.build_command(
|
||||
handler,
|
||||
{"configuration": "device.yaml", "port": "OTA", "no_states": False},
|
||||
)
|
||||
|
||||
assert "--no-states" not in cmd
|
||||
|
||||
|
||||
def _make_auth_handler(auth_header: str | None = None) -> Mock:
|
||||
"""Create a mock handler with the given Authorization header."""
|
||||
handler = Mock()
|
||||
|
||||
Reference in New Issue
Block a user