Fix mobile app regression accepting sensor states (#88164)
* Fix mobile app regression accepting sensor states * Adjust tests
This commit is contained in:
parent
9030ca05b1
commit
b21bf8763e
2 changed files with 41 additions and 10 deletions
|
@ -14,7 +14,7 @@ from nacl.secret import SecretBox
|
|||
from homeassistant.const import ATTR_DEVICE_ID, CONTENT_TYPE_JSON
|
||||
from homeassistant.core import Context, HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.json import JSONEncoder, JsonObjectType, json_loads_object
|
||||
from homeassistant.helpers.json import JSONEncoder, JsonValueType, json_loads
|
||||
|
||||
from .const import (
|
||||
ATTR_APP_DATA,
|
||||
|
@ -71,7 +71,7 @@ def _decrypt_payload_helper(
|
|||
ciphertext: str,
|
||||
get_key_bytes: Callable[[str, int], str | bytes],
|
||||
key_encoder,
|
||||
) -> JsonObjectType | None:
|
||||
) -> JsonValueType | None:
|
||||
"""Decrypt encrypted payload."""
|
||||
try:
|
||||
keylen, decrypt = setup_decrypt(key_encoder)
|
||||
|
@ -86,12 +86,12 @@ def _decrypt_payload_helper(
|
|||
key_bytes = get_key_bytes(key, keylen)
|
||||
|
||||
msg_bytes = decrypt(ciphertext, key_bytes)
|
||||
message = json_loads_object(msg_bytes)
|
||||
message = json_loads(msg_bytes)
|
||||
_LOGGER.debug("Successfully decrypted mobile_app payload")
|
||||
return message
|
||||
|
||||
|
||||
def _decrypt_payload(key: str | None, ciphertext: str) -> JsonObjectType | None:
|
||||
def _decrypt_payload(key: str | None, ciphertext: str) -> JsonValueType | None:
|
||||
"""Decrypt encrypted payload."""
|
||||
|
||||
def get_key_bytes(key: str, keylen: int) -> str:
|
||||
|
@ -100,7 +100,7 @@ def _decrypt_payload(key: str | None, ciphertext: str) -> JsonObjectType | None:
|
|||
return _decrypt_payload_helper(key, ciphertext, get_key_bytes, HexEncoder)
|
||||
|
||||
|
||||
def _decrypt_payload_legacy(key: str | None, ciphertext: str) -> JsonObjectType | None:
|
||||
def _decrypt_payload_legacy(key: str | None, ciphertext: str) -> JsonValueType | None:
|
||||
"""Decrypt encrypted payload."""
|
||||
|
||||
def get_key_bytes(key: str, keylen: int) -> bytes:
|
||||
|
|
|
@ -1090,6 +1090,21 @@ async def test_sending_sensor_state(
|
|||
|
||||
assert reg_resp.status == HTTPStatus.CREATED
|
||||
|
||||
reg_resp = await webhook_client.post(
|
||||
webhook_url,
|
||||
json={
|
||||
"type": "register_sensor",
|
||||
"data": {
|
||||
"name": "Battery Health",
|
||||
"state": "good",
|
||||
"type": "sensor",
|
||||
"unique_id": "health-id",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
assert reg_resp.status == HTTPStatus.CREATED
|
||||
|
||||
ent_reg = er.async_get(hass)
|
||||
entry = ent_reg.async_get("sensor.test_1_battery_state")
|
||||
assert entry.original_name == "Test 1 Battery State"
|
||||
|
@ -1105,15 +1120,27 @@ async def test_sending_sensor_state(
|
|||
assert state is not None
|
||||
assert state.state == "100"
|
||||
|
||||
state = hass.states.get("sensor.test_1_battery_health")
|
||||
assert state is not None
|
||||
assert state.state == "good"
|
||||
|
||||
# Now with a list.
|
||||
reg_resp = await webhook_client.post(
|
||||
webhook_url,
|
||||
json={
|
||||
"type": "update_sensor_states",
|
||||
"data": {
|
||||
"state": 50.0000,
|
||||
"type": "sensor",
|
||||
"unique_id": "abcd",
|
||||
},
|
||||
"data": [
|
||||
{
|
||||
"state": 50.0000,
|
||||
"type": "sensor",
|
||||
"unique_id": "abcd",
|
||||
},
|
||||
{
|
||||
"state": "okay-ish",
|
||||
"type": "sensor",
|
||||
"unique_id": "health-id",
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
|
@ -1122,3 +1149,7 @@ async def test_sending_sensor_state(
|
|||
state = hass.states.get("sensor.test_1_battery_state")
|
||||
assert state is not None
|
||||
assert state.state == "50.0"
|
||||
|
||||
state = hass.states.get("sensor.test_1_battery_health")
|
||||
assert state is not None
|
||||
assert state.state == "okay-ish"
|
||||
|
|
Loading…
Add table
Reference in a new issue