mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 02:07:54 +00:00
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
@ -14,7 +14,7 @@ from nacl.secret import SecretBox
|
|||||||
from homeassistant.const import ATTR_DEVICE_ID, CONTENT_TYPE_JSON
|
from homeassistant.const import ATTR_DEVICE_ID, CONTENT_TYPE_JSON
|
||||||
from homeassistant.core import Context, HomeAssistant
|
from homeassistant.core import Context, HomeAssistant
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
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 (
|
from .const import (
|
||||||
ATTR_APP_DATA,
|
ATTR_APP_DATA,
|
||||||
@ -71,7 +71,7 @@ def _decrypt_payload_helper(
|
|||||||
ciphertext: str,
|
ciphertext: str,
|
||||||
get_key_bytes: Callable[[str, int], str | bytes],
|
get_key_bytes: Callable[[str, int], str | bytes],
|
||||||
key_encoder,
|
key_encoder,
|
||||||
) -> JsonObjectType | None:
|
) -> JsonValueType | None:
|
||||||
"""Decrypt encrypted payload."""
|
"""Decrypt encrypted payload."""
|
||||||
try:
|
try:
|
||||||
keylen, decrypt = setup_decrypt(key_encoder)
|
keylen, decrypt = setup_decrypt(key_encoder)
|
||||||
@ -86,12 +86,12 @@ def _decrypt_payload_helper(
|
|||||||
key_bytes = get_key_bytes(key, keylen)
|
key_bytes = get_key_bytes(key, keylen)
|
||||||
|
|
||||||
msg_bytes = decrypt(ciphertext, key_bytes)
|
msg_bytes = decrypt(ciphertext, key_bytes)
|
||||||
message = json_loads_object(msg_bytes)
|
message = json_loads(msg_bytes)
|
||||||
_LOGGER.debug("Successfully decrypted mobile_app payload")
|
_LOGGER.debug("Successfully decrypted mobile_app payload")
|
||||||
return message
|
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."""
|
"""Decrypt encrypted payload."""
|
||||||
|
|
||||||
def get_key_bytes(key: str, keylen: int) -> str:
|
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)
|
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."""
|
"""Decrypt encrypted payload."""
|
||||||
|
|
||||||
def get_key_bytes(key: str, keylen: int) -> bytes:
|
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
|
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)
|
ent_reg = er.async_get(hass)
|
||||||
entry = ent_reg.async_get("sensor.test_1_battery_state")
|
entry = ent_reg.async_get("sensor.test_1_battery_state")
|
||||||
assert entry.original_name == "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 is not None
|
||||||
assert state.state == "100"
|
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(
|
reg_resp = await webhook_client.post(
|
||||||
webhook_url,
|
webhook_url,
|
||||||
json={
|
json={
|
||||||
"type": "update_sensor_states",
|
"type": "update_sensor_states",
|
||||||
"data": {
|
"data": [
|
||||||
"state": 50.0000,
|
{
|
||||||
"type": "sensor",
|
"state": 50.0000,
|
||||||
"unique_id": "abcd",
|
"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")
|
state = hass.states.get("sensor.test_1_battery_state")
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state == "50.0"
|
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…
x
Reference in New Issue
Block a user