2019-03-14 17:24:53 -07:00
|
|
|
"""Entity tests for mobile_app."""
|
2019-11-16 11:22:07 +02:00
|
|
|
|
2019-03-14 17:24:53 -07:00
|
|
|
import logging
|
|
|
|
|
2020-06-08 19:07:05 +02:00
|
|
|
from homeassistant.const import STATE_UNKNOWN, UNIT_PERCENTAGE
|
2019-11-27 20:27:22 -08:00
|
|
|
from homeassistant.helpers import device_registry
|
|
|
|
|
2019-03-14 17:24:53 -07:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-11-16 11:22:07 +02:00
|
|
|
async def test_sensor(hass, create_registrations, webhook_client):
|
2019-03-14 17:24:53 -07:00
|
|
|
"""Test that sensors can be registered and updated."""
|
2019-07-31 12:25:30 -07:00
|
|
|
webhook_id = create_registrations[1]["webhook_id"]
|
2020-04-05 00:26:08 +02:00
|
|
|
webhook_url = f"/api/webhook/{webhook_id}"
|
2019-03-14 17:24:53 -07:00
|
|
|
|
|
|
|
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",
|
2020-02-28 20:46:48 +01:00
|
|
|
"unit_of_measurement": UNIT_PERCENTAGE,
|
2019-07-31 12:25:30 -07:00
|
|
|
},
|
|
|
|
},
|
2019-03-14 17:24:53 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
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-03-14 17:24:53 -07:00
|
|
|
|
2020-02-12 11:40:39 -08:00
|
|
|
entity = hass.states.get("sensor.test_1_battery_state")
|
2019-03-15 07:47:13 -07:00
|
|
|
assert entity is not None
|
2019-03-14 17:24:53 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
assert entity.attributes["device_class"] == "battery"
|
|
|
|
assert entity.attributes["icon"] == "mdi:battery"
|
2020-02-28 20:46:48 +01:00
|
|
|
assert entity.attributes["unit_of_measurement"] == UNIT_PERCENTAGE
|
2019-07-31 12:25:30 -07:00
|
|
|
assert entity.attributes["foo"] == "bar"
|
|
|
|
assert entity.domain == "sensor"
|
2020-02-12 11:40:39 -08:00
|
|
|
assert entity.name == "Test 1 Battery State"
|
2019-07-31 12:25:30 -07:00
|
|
|
assert entity.state == "100"
|
2019-03-14 17:24:53 -07:00
|
|
|
|
|
|
|
update_resp = await webhook_client.post(
|
|
|
|
webhook_url,
|
|
|
|
json={
|
2019-07-31 12:25:30 -07:00
|
|
|
"type": "update_sensor_states",
|
|
|
|
"data": [
|
2019-03-14 17:24:53 -07:00
|
|
|
{
|
2019-07-31 12:25:30 -07:00
|
|
|
"icon": "mdi:battery-unknown",
|
|
|
|
"state": 123,
|
|
|
|
"type": "sensor",
|
|
|
|
"unique_id": "battery_state",
|
2020-06-04 01:13:01 -07:00
|
|
|
},
|
|
|
|
# This invalid data should not invalidate whole request
|
|
|
|
{"type": "sensor", "unique_id": "invalid_state", "invalid": "data"},
|
2019-07-31 12:25:30 -07:00
|
|
|
],
|
|
|
|
},
|
2019-03-14 17:24:53 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
assert update_resp.status == 200
|
|
|
|
|
2020-06-04 01:13:01 -07:00
|
|
|
json = await update_resp.json()
|
|
|
|
assert json["invalid_state"]["success"] is False
|
|
|
|
|
2020-02-12 11:40:39 -08:00
|
|
|
updated_entity = hass.states.get("sensor.test_1_battery_state")
|
2019-07-31 12:25:30 -07:00
|
|
|
assert updated_entity.state == "123"
|
2019-03-14 17:24:53 -07:00
|
|
|
|
2019-11-27 20:27:22 -08:00
|
|
|
dev_reg = await device_registry.async_get_registry(hass)
|
|
|
|
assert len(dev_reg.devices) == len(create_registrations)
|
|
|
|
|
2019-03-14 17:24:53 -07:00
|
|
|
|
2019-11-16 11:22:07 +02:00
|
|
|
async def test_sensor_must_register(hass, create_registrations, webhook_client):
|
2019-03-14 17:24:53 -07:00
|
|
|
"""Test that sensors must be registered before updating."""
|
2019-07-31 12:25:30 -07:00
|
|
|
webhook_id = create_registrations[1]["webhook_id"]
|
2020-04-05 00:26:08 +02:00
|
|
|
webhook_url = f"/api/webhook/{webhook_id}"
|
2019-03-14 17:24:53 -07:00
|
|
|
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"}],
|
|
|
|
},
|
2019-03-14 17:24:53 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
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-03-14 17:24:53 -07:00
|
|
|
|
|
|
|
|
2020-06-08 21:11:37 +02:00
|
|
|
async def test_sensor_id_no_dupes(hass, create_registrations, webhook_client, caplog):
|
|
|
|
"""Test that a duplicate unique ID in registration updates the sensor."""
|
2019-07-31 12:25:30 -07:00
|
|
|
webhook_id = create_registrations[1]["webhook_id"]
|
2020-04-05 00:26:08 +02:00
|
|
|
webhook_url = f"/api/webhook/{webhook_id}"
|
2019-03-14 17:24:53 -07:00
|
|
|
|
|
|
|
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",
|
2020-02-28 20:46:48 +01:00
|
|
|
"unit_of_measurement": UNIT_PERCENTAGE,
|
2019-07-31 12:25:30 -07:00
|
|
|
},
|
2019-03-14 17:24:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
2020-06-08 21:11:37 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert "Re-register existing sensor" not in caplog.text
|
|
|
|
|
|
|
|
entity = hass.states.get("sensor.test_1_battery_state")
|
|
|
|
assert entity is not None
|
|
|
|
|
|
|
|
assert entity.attributes["device_class"] == "battery"
|
|
|
|
assert entity.attributes["icon"] == "mdi:battery"
|
|
|
|
assert entity.attributes["unit_of_measurement"] == UNIT_PERCENTAGE
|
|
|
|
assert entity.attributes["foo"] == "bar"
|
|
|
|
assert entity.domain == "sensor"
|
|
|
|
assert entity.name == "Test 1 Battery State"
|
|
|
|
assert entity.state == "100"
|
2019-03-14 17:24:53 -07:00
|
|
|
|
2020-06-08 21:11:37 +02:00
|
|
|
payload["data"]["state"] = 99
|
2019-03-14 17:24:53 -07:00
|
|
|
dupe_resp = await webhook_client.post(webhook_url, json=payload)
|
|
|
|
|
2020-06-08 21:11:37 +02:00
|
|
|
assert dupe_resp.status == 201
|
|
|
|
dupe_reg_json = await dupe_resp.json()
|
|
|
|
assert dupe_reg_json == {"success": True}
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert "Re-register existing sensor" in caplog.text
|
|
|
|
|
|
|
|
entity = hass.states.get("sensor.test_1_battery_state")
|
|
|
|
assert entity is not None
|
2019-03-14 17:24:53 -07:00
|
|
|
|
2020-06-08 21:11:37 +02:00
|
|
|
assert entity.attributes["device_class"] == "battery"
|
|
|
|
assert entity.attributes["icon"] == "mdi:battery"
|
|
|
|
assert entity.attributes["unit_of_measurement"] == UNIT_PERCENTAGE
|
|
|
|
assert entity.attributes["foo"] == "bar"
|
|
|
|
assert entity.domain == "sensor"
|
|
|
|
assert entity.name == "Test 1 Battery State"
|
|
|
|
assert entity.state == "99"
|
2020-06-08 19:07:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_register_sensor_no_state(hass, create_registrations, webhook_client):
|
|
|
|
"""Test that sensors can be registered, when there is no (unknown) state."""
|
|
|
|
webhook_id = create_registrations[1]["webhook_id"]
|
|
|
|
webhook_url = f"/api/webhook/{webhook_id}"
|
|
|
|
|
|
|
|
reg_resp = await webhook_client.post(
|
|
|
|
webhook_url,
|
|
|
|
json={
|
|
|
|
"type": "register_sensor",
|
|
|
|
"data": {
|
|
|
|
"name": "Battery State",
|
|
|
|
"state": None,
|
|
|
|
"type": "sensor",
|
|
|
|
"unique_id": "battery_state",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert reg_resp.status == 201
|
|
|
|
|
|
|
|
json = await reg_resp.json()
|
|
|
|
assert json == {"success": True}
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
entity = hass.states.get("sensor.test_1_battery_state")
|
|
|
|
assert entity is not None
|
|
|
|
|
|
|
|
assert entity.domain == "sensor"
|
|
|
|
assert entity.name == "Test 1 Battery State"
|
|
|
|
assert entity.state == STATE_UNKNOWN
|
|
|
|
|
2020-06-09 20:06:52 +02:00
|
|
|
reg_resp = await webhook_client.post(
|
|
|
|
webhook_url,
|
|
|
|
json={
|
|
|
|
"type": "register_sensor",
|
|
|
|
"data": {
|
|
|
|
"name": "Backup Battery State",
|
|
|
|
"type": "sensor",
|
|
|
|
"unique_id": "backup_battery_state",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert reg_resp.status == 201
|
|
|
|
|
|
|
|
json = await reg_resp.json()
|
|
|
|
assert json == {"success": True}
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
entity = hass.states.get("sensor.test_1_backup_battery_state")
|
|
|
|
assert entity
|
|
|
|
|
|
|
|
assert entity.domain == "sensor"
|
|
|
|
assert entity.name == "Test 1 Backup Battery State"
|
|
|
|
assert entity.state == STATE_UNKNOWN
|
|
|
|
|
2020-06-08 19:07:05 +02:00
|
|
|
|
|
|
|
async def test_update_sensor_no_state(hass, create_registrations, webhook_client):
|
|
|
|
"""Test that sensors can be updated, when there is no (unknown) state."""
|
|
|
|
webhook_id = create_registrations[1]["webhook_id"]
|
|
|
|
webhook_url = f"/api/webhook/{webhook_id}"
|
|
|
|
|
|
|
|
reg_resp = await webhook_client.post(
|
|
|
|
webhook_url,
|
|
|
|
json={
|
|
|
|
"type": "register_sensor",
|
|
|
|
"data": {
|
|
|
|
"name": "Battery State",
|
|
|
|
"state": 100,
|
|
|
|
"type": "sensor",
|
|
|
|
"unique_id": "battery_state",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert reg_resp.status == 201
|
|
|
|
|
|
|
|
json = await reg_resp.json()
|
|
|
|
assert json == {"success": True}
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
entity = hass.states.get("sensor.test_1_battery_state")
|
|
|
|
assert entity is not None
|
|
|
|
assert entity.state == "100"
|
|
|
|
|
|
|
|
update_resp = await webhook_client.post(
|
|
|
|
webhook_url,
|
|
|
|
json={
|
|
|
|
"type": "update_sensor_states",
|
|
|
|
"data": [{"state": None, "type": "sensor", "unique_id": "battery_state"}],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert update_resp.status == 200
|
|
|
|
|
|
|
|
json = await update_resp.json()
|
|
|
|
assert json == {"battery_state": {"success": True}}
|
|
|
|
|
|
|
|
updated_entity = hass.states.get("sensor.test_1_battery_state")
|
|
|
|
assert updated_entity.state == STATE_UNKNOWN
|