mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
fix(hass): hass_get_state 的亮度按百分比报
HA 的 brightness 量程是 0~255,原样交给模型会被念成"一百五十三";灯关着时是 null,还会报"亮度是:None"。换算成百分比,和设置亮度用的 brightness_pct 同一个单位,开着的灯最低报 1%(同 HA 前端);不是数字时不报这一项。附测试。
This commit is contained in:
parent
c8e60d1325
commit
24d12d65e9
2 changed files with 77 additions and 7 deletions
|
|
@ -142,13 +142,12 @@ async def handle_hass_get_state(conn: "ConnectionHandler", entity_id):
|
|||
+ str(response.json()["attributes"]["rgb_color"])
|
||||
+ " "
|
||||
)
|
||||
if "brightness" in response.json()["attributes"]:
|
||||
responsetext = (
|
||||
responsetext
|
||||
+ "亮度是:"
|
||||
+ str(response.json()["attributes"]["brightness"])
|
||||
+ " "
|
||||
)
|
||||
brightness = response.json()["attributes"].get("brightness")
|
||||
if isinstance(brightness, (int, float)):
|
||||
# HA 的 brightness 量程是 0~255,原样报出去会被念成"一百五十三";
|
||||
# 换算成百分比,和设置亮度(brightness_pct,1~100)同一个单位。开着的灯最低报 1%(同 HA 前端)
|
||||
percent = max(1, round(brightness * 100 / 255)) if brightness > 0 else 0
|
||||
responsetext = responsetext + "亮度是:" + f"{percent}%" + " "
|
||||
logger.bind(tag=TAG).info(f"查询返回内容: {responsetext}")
|
||||
return responsetext
|
||||
else:
|
||||
|
|
|
|||
71
main/xiaozhi-server/tests/plugins_func/test_hass_state.py
Normal file
71
main/xiaozhi-server/tests/plugins_func/test_hass_state.py
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
"""hass_get_state 交回的亮度按百分比报。
|
||||
|
||||
HA 的 brightness 量程是 0~255,原样报会被念成"一百五十三";灯关着时是 null,以前还会报"亮度是:None"。
|
||||
HTTP 层换成假的客户端,不出网。
|
||||
"""
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from plugins_func.functions import hass_state as hass_module
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, payload, status_code=200):
|
||||
self.payload = payload
|
||||
self.status_code = status_code
|
||||
|
||||
def json(self):
|
||||
return self.payload
|
||||
|
||||
|
||||
class FakeAsyncClient:
|
||||
payload = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *exc_info):
|
||||
return False
|
||||
|
||||
async def get(self, url, headers=None):
|
||||
return FakeResponse(FakeAsyncClient.payload)
|
||||
|
||||
|
||||
class FakeConn:
|
||||
def __init__(self):
|
||||
self.load_function_plugin = True
|
||||
self.config = {
|
||||
"plugins": {
|
||||
"home_assistant": {
|
||||
"base_url": "http://homeassistant.local:8123",
|
||||
"api_key": "test-token",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_http(monkeypatch):
|
||||
monkeypatch.setattr(httpx, "AsyncClient", FakeAsyncClient)
|
||||
return FakeAsyncClient
|
||||
|
||||
|
||||
async def _get_state(fake_http, attributes, state="on"):
|
||||
fake_http.payload = {"state": state, "attributes": attributes}
|
||||
return await hass_module.handle_hass_get_state(FakeConn(), "light.desk_lamp")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("raw, shown", [(153, "60%"), (255, "100%"), (128, "50%"), (1, "1%"), (0, "0%")])
|
||||
async def test_brightness_reported_as_percent(fake_http, raw, shown):
|
||||
text = await _get_state(fake_http, {"brightness": raw, "color_temp_kelvin": 4000})
|
||||
assert f"亮度是:{shown} " in text
|
||||
assert text.startswith("设备状态:on ") and "色温是:4000 " in text
|
||||
|
||||
|
||||
async def test_no_brightness_line_when_off_or_missing(fake_http):
|
||||
# 灯关着时 HA 给 brightness: null
|
||||
assert await _get_state(fake_http, {"brightness": None}, state="off") == "设备状态:off "
|
||||
assert "亮度" not in await _get_state(fake_http, {"friendly_name": "台灯"})
|
||||
Loading…
Reference in a new issue