hass-core/tests/components/mobile_app/test_entity.py

126 lines
3.7 KiB
Python
Raw Normal View History

"""Entity tests for mobile_app."""
# pylint: disable=redefined-outer-name,unused-import
import logging
_LOGGER = logging.getLogger(__name__)
2019-07-31 12:25:30 -07:00
async def test_sensor(
hass, create_registrations, webhook_client
): # noqa: F401, F811, E501
"""Test that sensors can be registered and updated."""
2019-07-31 12:25:30 -07:00
webhook_id = create_registrations[1]["webhook_id"]
webhook_url = "/api/webhook/{}".format(webhook_id)
reg_resp = await webhook_client.post(
webhook_url,
json={
2019-07-31 12:25:30 -07:00
"type": "register_sensor",
"data": {
"attributes": {"foo": "bar"},
"device_class": "battery",
"icon": "mdi:battery",
"name": "Battery State",
"state": 100,
"type": "sensor",
"unique_id": "battery_state",
"unit_of_measurement": "%",
},
},
)
assert reg_resp.status == 201
json = await reg_resp.json()
2019-07-31 12:25:30 -07:00
assert json == {"success": True}
2019-04-25 00:55:37 +08:00
await hass.async_block_till_done()
2019-07-31 12:25:30 -07:00
entity = hass.states.get("sensor.battery_state")
2019-03-15 07:47:13 -07:00
assert entity is not None
2019-07-31 12:25:30 -07:00
assert entity.attributes["device_class"] == "battery"
assert entity.attributes["icon"] == "mdi:battery"
assert entity.attributes["unit_of_measurement"] == "%"
assert entity.attributes["foo"] == "bar"
assert entity.domain == "sensor"
assert entity.name == "Battery State"
assert entity.state == "100"
update_resp = await webhook_client.post(
webhook_url,
json={
2019-07-31 12:25:30 -07:00
"type": "update_sensor_states",
"data": [
{
2019-07-31 12:25:30 -07:00
"icon": "mdi:battery-unknown",
"state": 123,
"type": "sensor",
"unique_id": "battery_state",
}
2019-07-31 12:25:30 -07:00
],
},
)
assert update_resp.status == 200
2019-07-31 12:25:30 -07:00
updated_entity = hass.states.get("sensor.battery_state")
assert updated_entity.state == "123"
2019-07-31 12:25:30 -07:00
async def test_sensor_must_register(
hass, create_registrations, webhook_client # noqa: F401, F811, E501
): # noqa: F401, F811, E501
"""Test that sensors must be registered before updating."""
2019-07-31 12:25:30 -07:00
webhook_id = create_registrations[1]["webhook_id"]
webhook_url = "/api/webhook/{}".format(webhook_id)
resp = await webhook_client.post(
webhook_url,
json={
2019-07-31 12:25:30 -07:00
"type": "update_sensor_states",
"data": [{"state": 123, "type": "sensor", "unique_id": "battery_state"}],
},
)
assert resp.status == 200
json = await resp.json()
2019-07-31 12:25:30 -07:00
assert json["battery_state"]["success"] is False
assert json["battery_state"]["error"]["code"] == "not_registered"
2019-07-31 12:25:30 -07:00
async def test_sensor_id_no_dupes(
hass, create_registrations, webhook_client # noqa: F401, F811, E501
): # noqa: F401, F811, E501
"""Test that sensors must have a unique ID."""
2019-07-31 12:25:30 -07:00
webhook_id = create_registrations[1]["webhook_id"]
webhook_url = "/api/webhook/{}".format(webhook_id)
payload = {
2019-07-31 12:25:30 -07:00
"type": "register_sensor",
"data": {
"attributes": {"foo": "bar"},
"device_class": "battery",
"icon": "mdi:battery",
"name": "Battery State",
"state": 100,
"type": "sensor",
"unique_id": "battery_state",
"unit_of_measurement": "%",
},
}
reg_resp = await webhook_client.post(webhook_url, json=payload)
assert reg_resp.status == 201
reg_json = await reg_resp.json()
2019-07-31 12:25:30 -07:00
assert reg_json == {"success": True}
dupe_resp = await webhook_client.post(webhook_url, json=payload)
assert dupe_resp.status == 409
dupe_json = await dupe_resp.json()
2019-07-31 12:25:30 -07:00
assert dupe_json["success"] is False
assert dupe_json["error"]["code"] == "duplicate_unique_id"