mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
补充调整
This commit is contained in:
parent
b4e144afef
commit
dd367c9e43
2 changed files with 5 additions and 80 deletions
|
|
@ -121,13 +121,10 @@ async def handle_hass_get_state(conn: "ConnectionHandler", entity_id):
|
||||||
+ str(response.json()["attributes"]["media_title"])
|
+ str(response.json()["attributes"]["media_title"])
|
||||||
+ " "
|
+ " "
|
||||||
)
|
)
|
||||||
if "volume_level" in response.json()["attributes"]:
|
volume_level = response.json()["attributes"].get("volume_level")
|
||||||
responsetext = (
|
if isinstance(volume_level, (int, float)):
|
||||||
responsetext
|
# volume_level 量程 0~1,换算成百分比(同 volume_set 的 1~100)
|
||||||
+ "音量是:"
|
responsetext = responsetext + "音量是:" + f"{round(volume_level * 100)}%" + " "
|
||||||
+ str(response.json()["attributes"]["volume_level"])
|
|
||||||
+ " "
|
|
||||||
)
|
|
||||||
if "color_temp_kelvin" in response.json()["attributes"]:
|
if "color_temp_kelvin" in response.json()["attributes"]:
|
||||||
responsetext = (
|
responsetext = (
|
||||||
responsetext
|
responsetext
|
||||||
|
|
@ -144,8 +141,7 @@ async def handle_hass_get_state(conn: "ConnectionHandler", entity_id):
|
||||||
)
|
)
|
||||||
brightness = response.json()["attributes"].get("brightness")
|
brightness = response.json()["attributes"].get("brightness")
|
||||||
if isinstance(brightness, (int, float)):
|
if isinstance(brightness, (int, float)):
|
||||||
# HA 的 brightness 量程是 0~255,原样报出去会被念成"一百五十三";
|
# brightness 量程 0~255,换算成百分比(同 brightness_pct 的 1~100),开灯最低报 1%
|
||||||
# 换算成百分比,和设置亮度(brightness_pct,1~100)同一个单位。开着的灯最低报 1%(同 HA 前端)
|
|
||||||
percent = max(1, round(brightness * 100 / 255)) if brightness > 0 else 0
|
percent = max(1, round(brightness * 100 / 255)) if brightness > 0 else 0
|
||||||
responsetext = responsetext + "亮度是:" + f"{percent}%" + " "
|
responsetext = responsetext + "亮度是:" + f"{percent}%" + " "
|
||||||
logger.bind(tag=TAG).info(f"查询返回内容: {responsetext}")
|
logger.bind(tag=TAG).info(f"查询返回内容: {responsetext}")
|
||||||
|
|
|
||||||
|
|
@ -1,71 +0,0 @@
|
||||||
"""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