2018-03-15 02:48:21 +01:00
|
|
|
"""Test HomeKit util module."""
|
2018-04-12 21:01:41 +08:00
|
|
|
import pytest
|
2018-05-11 01:21:59 +02:00
|
|
|
import voluptuous as vol
|
2018-03-15 02:48:21 +01:00
|
|
|
|
|
|
|
from homeassistant.components.homekit.accessories import HomeBridge
|
|
|
|
from homeassistant.components.homekit.const import HOMEKIT_NOTIFY_ID
|
|
|
|
from homeassistant.components.homekit.util import (
|
2018-03-27 11:31:18 +02:00
|
|
|
show_setup_message, dismiss_setup_message, convert_to_float,
|
2018-05-11 01:21:59 +02:00
|
|
|
temperature_to_homekit, temperature_to_states, density_to_air_quality)
|
2018-03-15 02:48:21 +01:00
|
|
|
from homeassistant.components.homekit.util import validate_entity_config \
|
|
|
|
as vec
|
|
|
|
from homeassistant.components.persistent_notification import (
|
2018-05-11 01:21:59 +02:00
|
|
|
DOMAIN, ATTR_NOTIFICATION_ID)
|
2018-03-15 02:48:21 +01:00
|
|
|
from homeassistant.const import (
|
2018-05-11 01:21:59 +02:00
|
|
|
ATTR_CODE, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
2018-03-15 02:48:21 +01:00
|
|
|
|
2018-05-11 01:21:59 +02:00
|
|
|
from tests.common import async_mock_service
|
2018-03-15 02:48:21 +01:00
|
|
|
|
|
|
|
|
2018-04-12 21:01:41 +08:00
|
|
|
def test_validate_entity_config():
|
|
|
|
"""Test validate entities."""
|
|
|
|
configs = [{'invalid_entity_id': {}}, {'demo.test': 1},
|
|
|
|
{'demo.test': 'test'}, {'demo.test': [1, 2]},
|
|
|
|
{'demo.test': None}]
|
|
|
|
|
|
|
|
for conf in configs:
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
vec(conf)
|
|
|
|
|
|
|
|
assert vec({}) == {}
|
|
|
|
assert vec({'alarm_control_panel.demo': {ATTR_CODE: '1234'}}) == \
|
|
|
|
{'alarm_control_panel.demo': {ATTR_CODE: '1234'}}
|
|
|
|
|
|
|
|
|
|
|
|
def test_convert_to_float():
|
|
|
|
"""Test convert_to_float method."""
|
|
|
|
assert convert_to_float(12) == 12
|
|
|
|
assert convert_to_float(12.4) == 12.4
|
|
|
|
assert convert_to_float(STATE_UNKNOWN) is None
|
|
|
|
assert convert_to_float(None) is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_to_homekit():
|
|
|
|
"""Test temperature conversion from HA to HomeKit."""
|
|
|
|
assert temperature_to_homekit(20.46, TEMP_CELSIUS) == 20.5
|
|
|
|
assert temperature_to_homekit(92.1, TEMP_FAHRENHEIT) == 33.4
|
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_to_states():
|
|
|
|
"""Test temperature conversion from HomeKit to HA."""
|
|
|
|
assert temperature_to_states(20, TEMP_CELSIUS) == 20.0
|
|
|
|
assert temperature_to_states(20.2, TEMP_FAHRENHEIT) == 68.4
|
|
|
|
|
|
|
|
|
|
|
|
def test_density_to_air_quality():
|
|
|
|
"""Test map PM2.5 density to HomeKit AirQuality level."""
|
|
|
|
assert density_to_air_quality(0) == 1
|
|
|
|
assert density_to_air_quality(35) == 1
|
|
|
|
assert density_to_air_quality(35.1) == 2
|
|
|
|
assert density_to_air_quality(75) == 2
|
|
|
|
assert density_to_air_quality(115) == 3
|
|
|
|
assert density_to_air_quality(150) == 4
|
|
|
|
assert density_to_air_quality(300) == 5
|
|
|
|
|
|
|
|
|
2018-05-11 01:21:59 +02:00
|
|
|
async def test_show_setup_msg(hass):
|
|
|
|
"""Test show setup message as persistence notification."""
|
|
|
|
bridge = HomeBridge(hass)
|
|
|
|
|
|
|
|
call_create_notification = async_mock_service(hass, DOMAIN, 'create')
|
|
|
|
|
|
|
|
await hass.async_add_job(show_setup_message, hass, bridge)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert call_create_notification
|
|
|
|
assert call_create_notification[0].data[ATTR_NOTIFICATION_ID] == \
|
|
|
|
HOMEKIT_NOTIFY_ID
|
|
|
|
|
|
|
|
|
|
|
|
async def test_dismiss_setup_msg(hass):
|
|
|
|
"""Test dismiss setup message."""
|
|
|
|
call_dismiss_notification = async_mock_service(hass, DOMAIN, 'dismiss')
|
|
|
|
|
|
|
|
await hass.async_add_job(dismiss_setup_message, hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert call_dismiss_notification
|
|
|
|
assert call_dismiss_notification[0].data[ATTR_NOTIFICATION_ID] == \
|
|
|
|
HOMEKIT_NOTIFY_ID
|