Mobile App: Sensors (#21854)
## Description: **Related issue (if applicable):** fixes #21782 ## Checklist: - [x] The code change is tested and works locally. - [x] Local tests pass with `tox`. **Your PR cannot be merged unless tests pass** - [x] There is no commented out code in this PR.
This commit is contained in:
1 parent
6a80ffa8cc
commit
dcaced1966
9 files changed
+529
-30
No files matched your search
@@ -6,9 +6,9 @@ from tests.common import mock_device_registry
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from homeassistant.components.mobile_app.const import (DATA_CONFIG_ENTRIES,
|
||||
from homeassistant.components.mobile_app.const import (DATA_BINARY_SENSOR,
|
||||
DATA_DELETED_IDS,
|
||||
DATA_DEVICES,
|
||||
DATA_SENSOR,
|
||||
DOMAIN,
|
||||
STORAGE_KEY,
|
||||
STORAGE_VERSION)
|
||||
@@ -48,7 +48,9 @@ async def webhook_client(hass, aiohttp_client, hass_storage, hass_admin_user):
|
||||
hass_storage[STORAGE_KEY] = {
|
||||
'version': STORAGE_VERSION,
|
||||
'data': {
|
||||
DATA_CONFIG_ENTRIES: {}, DATA_DELETED_IDS: [], DATA_DEVICES: {},
|
||||
DATA_BINARY_SENSOR: {},
|
||||
DATA_DELETED_IDS: [],
|
||||
DATA_SENSOR: {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
"""Entity tests for mobile_app."""
|
||||
# pylint: disable=redefined-outer-name,unused-import
|
||||
import logging
|
||||
|
||||
from . import (authed_api_client, create_registrations, # noqa: F401
|
||||
webhook_client) # noqa: F401
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def test_sensor(hass, create_registrations, webhook_client): # noqa: F401, F811, E501
|
||||
"""Test that sensors can be registered and updated."""
|
||||
webhook_id = create_registrations[1]['webhook_id']
|
||||
webhook_url = '/api/webhook/{}'.format(webhook_id)
|
||||
|
||||
reg_resp = await webhook_client.post(
|
||||
webhook_url,
|
||||
json={
|
||||
'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()
|
||||
assert json == {'status': 'registered'}
|
||||
|
||||
# 3 because we require device_tracker which adds zone.home and
|
||||
# group.all_devices
|
||||
assert len(hass.states.async_all()) == 3
|
||||
|
||||
entity = hass.states.async_all()[2]
|
||||
|
||||
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={
|
||||
'type': 'update_sensor_states',
|
||||
'data': [
|
||||
{
|
||||
'icon': 'mdi:battery-unknown',
|
||||
'state': 123,
|
||||
'type': 'sensor',
|
||||
'unique_id': 'battery_state'
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
assert update_resp.status == 200
|
||||
|
||||
updated_entity = hass.states.async_all()[2]
|
||||
|
||||
assert updated_entity.state == '123'
|
||||
|
||||
|
||||
async def test_sensor_must_register(hass, create_registrations, # noqa: F401, F811, E501
|
||||
webhook_client): # noqa: F401, F811, E501
|
||||
"""Test that sensors must be registered before updating."""
|
||||
webhook_id = create_registrations[1]['webhook_id']
|
||||
webhook_url = '/api/webhook/{}'.format(webhook_id)
|
||||
resp = await webhook_client.post(
|
||||
webhook_url,
|
||||
json={
|
||||
'type': 'update_sensor_states',
|
||||
'data': [
|
||||
{
|
||||
'state': 123,
|
||||
'type': 'sensor',
|
||||
'unique_id': 'battery_state'
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
assert resp.status == 200
|
||||
|
||||
json = await resp.json()
|
||||
assert json['battery_state']['success'] is False
|
||||
assert json['battery_state']['error']['code'] == 'not_registered'
|
||||
|
||||
|
||||
async def test_sensor_id_no_dupes(hass, create_registrations, # noqa: F401, F811, E501
|
||||
webhook_client): # noqa: F401, F811, E501
|
||||
"""Test that sensors must have a unique ID."""
|
||||
webhook_id = create_registrations[1]['webhook_id']
|
||||
webhook_url = '/api/webhook/{}'.format(webhook_id)
|
||||
|
||||
payload = {
|
||||
'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()
|
||||
assert reg_json == {'status': 'registered'}
|
||||
|
||||
dupe_resp = await webhook_client.post(webhook_url, json=payload)
|
||||
|
||||
assert dupe_resp.status == 409
|
||||
|
||||
dupe_json = await dupe_resp.json()
|
||||
assert dupe_json['success'] is False
|
||||
assert dupe_json['error']['code'] == 'duplicate_unique_id'
|
||||
Reference in new issue
Block a user