Add support for outlets in HomeKit (#14628)

This commit is contained in:
Matt Schmitt authored and cdce8p committed 2018-06-01 18:04:54 +02:00
1 parent 0a724a5473
commit f5d74e07d5
7 files changed
+123 -22

No files matched your search

@@ -9,10 +9,11 @@ import homeassistant.components.climate as climate
import homeassistant.components.media_player as media_player
from homeassistant.components.homekit import get_accessory, TYPES
from homeassistant.components.homekit.const import (
CONF_FEATURE_LIST, FEATURE_ON_OFF)
CONF_FEATURE_LIST, FEATURE_ON_OFF, TYPE_OUTLET, TYPE_SWITCH)
from homeassistant.const import (
ATTR_CODE, ATTR_DEVICE_CLASS, ATTR_SUPPORTED_FEATURES,
ATTR_UNIT_OF_MEASUREMENT, CONF_NAME, TEMP_CELSIUS, TEMP_FAHRENHEIT)
ATTR_UNIT_OF_MEASUREMENT, CONF_NAME, CONF_TYPE, TEMP_CELSIUS,
TEMP_FAHRENHEIT)
def test_not_supported(caplog):
@@ -129,17 +130,19 @@ def test_type_sensors(type_name, entity_id, state, attrs):
assert mock_type.called
@pytest.mark.parametrize('type_name, entity_id, state, attrs', [
('Switch', 'automation.test', 'on', {}),
('Switch', 'input_boolean.test', 'on', {}),
('Switch', 'remote.test', 'on', {}),
('Switch', 'script.test', 'on', {}),
('Switch', 'switch.test', 'on', {}),
@pytest.mark.parametrize('type_name, entity_id, state, attrs, config', [
('Outlet', 'switch.test', 'on', {}, {CONF_TYPE: TYPE_OUTLET}),
('Switch', 'automation.test', 'on', {}, {}),
('Switch', 'input_boolean.test', 'on', {}, {}),
('Switch', 'remote.test', 'on', {}, {}),
('Switch', 'script.test', 'on', {}, {}),
('Switch', 'switch.test', 'on', {}, {}),
('Switch', 'switch.test', 'on', {}, {CONF_TYPE: TYPE_SWITCH}),
])
def test_type_switches(type_name, entity_id, state, attrs):
def test_type_switches(type_name, entity_id, state, attrs, config):
"""Test if switch types are associated correctly."""
mock_type = Mock()
with patch.dict(TYPES, {type_name: mock_type}):
entity_state = State(entity_id, state, attrs)
get_accessory(None, None, entity_state, 2, {})
get_accessory(None, None, entity_state, 2, config)
assert mock_type.called
+41 -1
View File
@@ -2,12 +2,51 @@
import pytest
from homeassistant.core import split_entity_id
from homeassistant.components.homekit.type_switches import Switch
from homeassistant.components.homekit.type_switches import Outlet, Switch
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
from tests.common import async_mock_service
async def test_outlet_set_state(hass, hk_driver):
"""Test if Outlet accessory and HA are updated accordingly."""
entity_id = 'switch.outlet_test'
hass.states.async_set(entity_id, None)
await hass.async_block_till_done()
acc = Outlet(hass, hk_driver, 'Outlet', entity_id, 2, None)
await hass.async_add_job(acc.run)
await hass.async_block_till_done()
assert acc.aid == 2
assert acc.category == 7 # Outlet
assert acc.char_on.value is False
assert acc.char_outlet_in_use.value is True
hass.states.async_set(entity_id, STATE_ON)
await hass.async_block_till_done()
assert acc.char_on.value is True
hass.states.async_set(entity_id, STATE_OFF)
await hass.async_block_till_done()
assert acc.char_on.value is False
# Set from HomeKit
call_turn_on = async_mock_service(hass, 'switch', 'turn_on')
call_turn_off = async_mock_service(hass, 'switch', 'turn_off')
await hass.async_add_job(acc.char_on.client_update_value, True)
await hass.async_block_till_done()
assert call_turn_on
assert call_turn_on[0].data[ATTR_ENTITY_ID] == entity_id
await hass.async_add_job(acc.char_on.client_update_value, False)
await hass.async_block_till_done()
assert call_turn_off
assert call_turn_off[0].data[ATTR_ENTITY_ID] == entity_id
@pytest.mark.parametrize('entity_id', [
'automation.test',
'input_boolean.test',
@@ -23,6 +62,7 @@ async def test_switch_set_state(hass, hk_driver, entity_id):
await hass.async_block_till_done()
acc = Switch(hass, hk_driver, 'Switch', entity_id, 2, None)
await hass.async_add_job(acc.run)
await hass.async_block_till_done()
assert acc.aid == 2
assert acc.category == 8 # Switch
+6 -3
View File
@@ -5,7 +5,7 @@ import voluptuous as vol
from homeassistant.core import State
from homeassistant.components.homekit.const import (
CONF_FEATURE, CONF_FEATURE_LIST, HOMEKIT_NOTIFY_ID, FEATURE_ON_OFF,
FEATURE_PLAY_PAUSE)
FEATURE_PLAY_PAUSE, TYPE_OUTLET)
from homeassistant.components.homekit.util import (
convert_to_float, density_to_air_quality, dismiss_setup_message,
show_setup_message, temperature_to_homekit, temperature_to_states,
@@ -15,7 +15,7 @@ from homeassistant.components.homekit.util import validate_entity_config \
from homeassistant.components.persistent_notification import (
ATTR_MESSAGE, ATTR_NOTIFICATION_ID, DOMAIN)
from homeassistant.const import (
ATTR_CODE, ATTR_SUPPORTED_FEATURES, CONF_NAME, STATE_UNKNOWN,
ATTR_CODE, ATTR_SUPPORTED_FEATURES, CONF_NAME, CONF_TYPE, STATE_UNKNOWN,
TEMP_CELSIUS, TEMP_FAHRENHEIT)
from tests.common import async_mock_service
@@ -30,7 +30,8 @@ def test_validate_entity_config():
{CONF_FEATURE: 'invalid_feature'}]}},
{'media_player.test': {CONF_FEATURE_LIST: [
{CONF_FEATURE: FEATURE_ON_OFF},
{CONF_FEATURE: FEATURE_ON_OFF}]}}, ]
{CONF_FEATURE: FEATURE_ON_OFF}]}},
{'switch.test': {CONF_TYPE: 'invalid_type'}}]
for conf in configs:
with pytest.raises(vol.Invalid):
@@ -56,6 +57,8 @@ def test_validate_entity_config():
assert vec({'media_player.demo': config}) == \
{'media_player.demo': {CONF_FEATURE_LIST:
{FEATURE_ON_OFF: {}, FEATURE_PLAY_PAUSE: {}}}}
assert vec({'switch.demo': {CONF_TYPE: TYPE_OUTLET}}) == \
{'switch.demo': {CONF_TYPE: TYPE_OUTLET}}
def test_validate_media_player_features():