1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00
This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -5,114 +5,108 @@ import logging
_LOGGER = logging.getLogger(__name__)
async def test_sensor(hass, create_registrations, webhook_client): # noqa: F401, F811, E501
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)
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': '%'
}
}
"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 == {'success': True}
assert json == {"success": True}
await hass.async_block_till_done()
entity = hass.states.get('sensor.battery_state')
entity = hass.states.get("sensor.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'] == '%'
assert entity.attributes['foo'] == 'bar'
assert entity.domain == 'sensor'
assert entity.name == 'Battery State'
assert entity.state == '100'
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': [
"type": "update_sensor_states",
"data": [
{
'icon': 'mdi:battery-unknown',
'state': 123,
'type': 'sensor',
'unique_id': 'battery_state'
"icon": "mdi:battery-unknown",
"state": 123,
"type": "sensor",
"unique_id": "battery_state",
}
]
}
],
},
)
assert update_resp.status == 200
updated_entity = hass.states.get('sensor.battery_state')
assert updated_entity.state == '123'
updated_entity = hass.states.get("sensor.battery_state")
assert updated_entity.state == "123"
async def test_sensor_must_register(hass, create_registrations, # noqa: F401, F811, E501
webhook_client): # noqa: F401, F811, E501
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."""
webhook_id = create_registrations[1]['webhook_id']
webhook_url = '/api/webhook/{}'.format(webhook_id)
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'
}
]
}
"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'
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
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."""
webhook_id = create_registrations[1]['webhook_id']
webhook_url = '/api/webhook/{}'.format(webhook_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': '%'
}
"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)
@@ -120,12 +114,12 @@ async def test_sensor_id_no_dupes(hass, create_registrations, # noqa: F401, F81
assert reg_resp.status == 201
reg_json = await reg_resp.json()
assert reg_json == {'success': True}
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()
assert dupe_json['success'] is False
assert dupe_json['error']['code'] == 'duplicate_unique_id'
assert dupe_json["success"] is False
assert dupe_json["error"]["code"] == "duplicate_unique_id"