From 722c27f1e2f83e723070e1aff9c826fd78c98f6a Mon Sep 17 00:00:00 2001 From: Matt Schmitt Date: Sun, 17 Jun 2018 07:37:44 -0400 Subject: [PATCH] HomeKit style clean up (#14793) --- homeassistant/components/homekit/__init__.py | 4 ++-- homeassistant/components/homekit/type_fans.py | 3 --- .../homekit/type_security_systems.py | 15 +++++++----- .../components/homekit/type_switches.py | 4 ++-- tests/components/homekit/test_accessories.py | 3 +-- .../homekit/test_get_accessories.py | 4 ++-- tests/components/homekit/test_homekit.py | 4 ++-- tests/components/homekit/test_type_covers.py | 2 +- tests/components/homekit/test_type_fans.py | 23 +++++++----------- tests/components/homekit/test_type_lights.py | 6 ++--- .../homekit/test_type_media_players.py | 24 +++++++++---------- tests/components/homekit/test_type_sensors.py | 6 ++--- .../components/homekit/test_type_switches.py | 2 +- .../homekit/test_type_thermostats.py | 4 ++-- tests/components/homekit/test_util.py | 2 +- 15 files changed, 47 insertions(+), 59 deletions(-) diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index 34372b8b6a8..cb9387fb2c0 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -107,8 +107,8 @@ def get_accessory(hass, driver, state, aid, config): a_type = 'Thermostat' elif state.domain == 'cover': - features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) device_class = state.attributes.get(ATTR_DEVICE_CLASS) + features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) if device_class == 'garage' and \ features & (cover.SUPPORT_OPEN | cover.SUPPORT_CLOSE): @@ -134,8 +134,8 @@ def get_accessory(hass, driver, state, aid, config): a_type = 'MediaPlayer' elif state.domain == 'sensor': - unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) device_class = state.attributes.get(ATTR_DEVICE_CLASS) + unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) if device_class == DEVICE_CLASS_TEMPERATURE or \ unit in (TEMP_CELSIUS, TEMP_FAHRENHEIT): diff --git a/homeassistant/components/homekit/type_fans.py b/homeassistant/components/homekit/type_fans.py index bf0d4da6a59..aa44b11fefb 100644 --- a/homeassistant/components/homekit/type_fans.py +++ b/homeassistant/components/homekit/type_fans.py @@ -57,9 +57,6 @@ class Fan(HomeAccessory): def set_state(self, value): """Set state if call came from HomeKit.""" - if self._state == value: - return - _LOGGER.debug('%s: Set state to %d', self.entity_id, value) self._flag[CHAR_ACTIVE] = True service = SERVICE_TURN_ON if value == 1 else SERVICE_TURN_OFF diff --git a/homeassistant/components/homekit/type_security_systems.py b/homeassistant/components/homekit/type_security_systems.py index bbf8b3f17cb..a7d36720cab 100644 --- a/homeassistant/components/homekit/type_security_systems.py +++ b/homeassistant/components/homekit/type_security_systems.py @@ -5,8 +5,10 @@ from pyhap.const import CATEGORY_ALARM_SYSTEM from homeassistant.components.alarm_control_panel import DOMAIN from homeassistant.const import ( - ATTR_ENTITY_ID, ATTR_CODE, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, - STATE_ALARM_ARMED_NIGHT, STATE_ALARM_TRIGGERED, STATE_ALARM_DISARMED) + ATTR_ENTITY_ID, ATTR_CODE, SERVICE_ALARM_ARM_AWAY, SERVICE_ALARM_ARM_HOME, + SERVICE_ALARM_ARM_NIGHT, SERVICE_ALARM_DISARM, STATE_ALARM_ARMED_AWAY, + STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_NIGHT, STATE_ALARM_TRIGGERED, + STATE_ALARM_DISARMED) from . import TYPES from .accessories import HomeAccessory @@ -22,10 +24,11 @@ HASS_TO_HOMEKIT = {STATE_ALARM_ARMED_HOME: 0, STATE_ALARM_DISARMED: 3, STATE_ALARM_TRIGGERED: 4} HOMEKIT_TO_HASS = {c: s for s, c in HASS_TO_HOMEKIT.items()} -STATE_TO_SERVICE = {STATE_ALARM_ARMED_HOME: 'alarm_arm_home', - STATE_ALARM_ARMED_AWAY: 'alarm_arm_away', - STATE_ALARM_ARMED_NIGHT: 'alarm_arm_night', - STATE_ALARM_DISARMED: 'alarm_disarm'} +STATE_TO_SERVICE = { + STATE_ALARM_ARMED_AWAY: SERVICE_ALARM_ARM_AWAY, + STATE_ALARM_ARMED_HOME: SERVICE_ALARM_ARM_HOME, + STATE_ALARM_ARMED_NIGHT: SERVICE_ALARM_ARM_NIGHT, + STATE_ALARM_DISARMED: SERVICE_ALARM_DISARM} @TYPES.register('SecuritySystem') diff --git a/homeassistant/components/homekit/type_switches.py b/homeassistant/components/homekit/type_switches.py index c8bf8c7ad7c..a5724057eee 100644 --- a/homeassistant/components/homekit/type_switches.py +++ b/homeassistant/components/homekit/type_switches.py @@ -3,7 +3,7 @@ import logging from pyhap.const import CATEGORY_OUTLET, CATEGORY_SWITCH -from homeassistant.components.switch import DOMAIN as SWITCH +from homeassistant.components.switch import DOMAIN from homeassistant.const import ( ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF, STATE_ON) from homeassistant.core import split_entity_id @@ -37,7 +37,7 @@ class Outlet(HomeAccessory): self.flag_target_state = True params = {ATTR_ENTITY_ID: self.entity_id} service = SERVICE_TURN_ON if value else SERVICE_TURN_OFF - self.hass.services.call(SWITCH, service, params) + self.hass.services.call(DOMAIN, service, params) def update_state(self, new_state): """Update switch state after state changed.""" diff --git a/tests/components/homekit/test_accessories.py b/tests/components/homekit/test_accessories.py index 711c38443f2..2ffdcb0830f 100644 --- a/tests/components/homekit/test_accessories.py +++ b/tests/components/homekit/test_accessories.py @@ -56,8 +56,7 @@ async def test_home_accessory(hass, hk_driver): hass.states.async_set(entity_id, None) await hass.async_block_till_done() - acc = HomeAccessory(hass, hk_driver, 'Home Accessory', - entity_id, 2, None) + acc = HomeAccessory(hass, hk_driver, 'Home Accessory', entity_id, 2, None) assert acc.hass == hass assert acc.display_name == 'Home Accessory' assert acc.aid == 2 diff --git a/tests/components/homekit/test_get_accessories.py b/tests/components/homekit/test_get_accessories.py index 4de68057084..92f8736d1fe 100644 --- a/tests/components/homekit/test_get_accessories.py +++ b/tests/components/homekit/test_get_accessories.py @@ -34,7 +34,7 @@ def test_not_supported_media_player(): # selected mode for entity not supported config = {CONF_FEATURE_LIST: {FEATURE_ON_OFF: None}} entity_state = State('media_player.demo', 'on') - get_accessory(None, None, entity_state, 2, config) is None + assert get_accessory(None, None, entity_state, 2, config) is None # no supported modes for entity entity_state = State('media_player.demo', 'on') @@ -62,7 +62,7 @@ def test_customize_options(config, name): {ATTR_SUPPORTED_FEATURES: media_player.SUPPORT_TURN_ON | media_player.SUPPORT_TURN_OFF}, {CONF_FEATURE_LIST: {FEATURE_ON_OFF: None}}), - ('SecuritySystem', 'alarm_control_panel.test', 'armed', {}, + ('SecuritySystem', 'alarm_control_panel.test', 'armed_away', {}, {ATTR_CODE: '1234'}), ('Thermostat', 'climate.test', 'auto', {}, {}), ('Thermostat', 'climate.test', 'auto', diff --git a/tests/components/homekit/test_homekit.py b/tests/components/homekit/test_homekit.py index 08e8da7857e..cc0370f01b1 100644 --- a/tests/components/homekit/test_homekit.py +++ b/tests/components/homekit/test_homekit.py @@ -4,17 +4,17 @@ from unittest.mock import patch, ANY, Mock import pytest from homeassistant import setup -from homeassistant.core import State from homeassistant.components.homekit import ( generate_aid, HomeKit, STATUS_READY, STATUS_RUNNING, STATUS_STOPPED, STATUS_WAIT) from homeassistant.components.homekit.accessories import HomeBridge from homeassistant.components.homekit.const import ( CONF_AUTO_START, DEFAULT_PORT, DOMAIN, HOMEKIT_FILE, SERVICE_HOMEKIT_START) -from homeassistant.helpers.entityfilter import generate_filter from homeassistant.const import ( CONF_IP_ADDRESS, CONF_PORT, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP) +from homeassistant.core import State +from homeassistant.helpers.entityfilter import generate_filter from tests.components.homekit.common import patch_debounce diff --git a/tests/components/homekit/test_type_covers.py b/tests/components/homekit/test_type_covers.py index c69ddacd328..04ed5df5702 100644 --- a/tests/components/homekit/test_type_covers.py +++ b/tests/components/homekit/test_type_covers.py @@ -19,7 +19,7 @@ def cls(): patcher = patch_debounce() patcher.start() _import = __import__('homeassistant.components.homekit.type_covers', - fromlist=['GarageDoorOpener', 'WindowCovering,', + fromlist=['GarageDoorOpener', 'WindowCovering', 'WindowCoveringBasic']) patcher_tuple = namedtuple('Cls', ['window', 'window_basic', 'garage']) yield patcher_tuple(window=_import.WindowCovering, diff --git a/tests/components/homekit/test_type_fans.py b/tests/components/homekit/test_type_fans.py index ba7d4ccdcf0..87a481ff06f 100644 --- a/tests/components/homekit/test_type_fans.py +++ b/tests/components/homekit/test_type_fans.py @@ -5,11 +5,10 @@ import pytest from homeassistant.components.fan import ( ATTR_DIRECTION, ATTR_OSCILLATING, DIRECTION_FORWARD, DIRECTION_REVERSE, - DOMAIN, SERVICE_OSCILLATE, SERVICE_SET_DIRECTION, - SUPPORT_DIRECTION, SUPPORT_OSCILLATE) + DOMAIN, SUPPORT_DIRECTION, SUPPORT_OSCILLATE) from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, STATE_ON, STATE_OFF, - STATE_UNKNOWN, SERVICE_TURN_ON, SERVICE_TURN_OFF) + STATE_UNKNOWN) from tests.common import async_mock_service from tests.components.homekit.common import patch_debounce @@ -31,8 +30,7 @@ async def test_fan_basic(hass, hk_driver, cls): """Test fan with char state.""" entity_id = 'fan.demo' - hass.states.async_set(entity_id, STATE_ON, - {ATTR_SUPPORTED_FEATURES: 0}) + hass.states.async_set(entity_id, STATE_ON, {ATTR_SUPPORTED_FEATURES: 0}) await hass.async_block_till_done() acc = cls.fan(hass, hk_driver, 'Fan', entity_id, 2, None) @@ -44,8 +42,7 @@ async def test_fan_basic(hass, hk_driver, cls): await hass.async_block_till_done() assert acc.char_active.value == 1 - hass.states.async_set(entity_id, STATE_OFF, - {ATTR_SUPPORTED_FEATURES: 0}) + hass.states.async_set(entity_id, STATE_OFF, {ATTR_SUPPORTED_FEATURES: 0}) await hass.async_block_till_done() assert acc.char_active.value == 0 @@ -58,8 +55,8 @@ async def test_fan_basic(hass, hk_driver, cls): assert acc.char_active.value == 0 # Set from HomeKit - call_turn_on = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON) - call_turn_off = async_mock_service(hass, DOMAIN, SERVICE_TURN_OFF) + call_turn_on = async_mock_service(hass, DOMAIN, 'turn_on') + call_turn_off = async_mock_service(hass, DOMAIN, 'turn_off') await hass.async_add_job(acc.char_active.client_update_value, 1) await hass.async_block_till_done() @@ -97,8 +94,7 @@ async def test_fan_direction(hass, hk_driver, cls): assert acc.char_direction.value == 1 # Set from HomeKit - call_set_direction = async_mock_service(hass, DOMAIN, - SERVICE_SET_DIRECTION) + call_set_direction = async_mock_service(hass, DOMAIN, 'set_direction') await hass.async_add_job(acc.char_direction.client_update_value, 0) await hass.async_block_till_done() @@ -128,13 +124,12 @@ async def test_fan_oscillate(hass, hk_driver, cls): await hass.async_block_till_done() assert acc.char_swing.value == 0 - hass.states.async_set(entity_id, STATE_ON, - {ATTR_OSCILLATING: True}) + hass.states.async_set(entity_id, STATE_ON, {ATTR_OSCILLATING: True}) await hass.async_block_till_done() assert acc.char_swing.value == 1 # Set from HomeKit - call_oscillate = async_mock_service(hass, DOMAIN, SERVICE_OSCILLATE) + call_oscillate = async_mock_service(hass, DOMAIN, 'oscillate') await hass.async_add_job(acc.char_swing.client_update_value, 0) await hass.async_block_till_done() diff --git a/tests/components/homekit/test_type_lights.py b/tests/components/homekit/test_type_lights.py index a9a5f1c3ece..aab6274f484 100644 --- a/tests/components/homekit/test_type_lights.py +++ b/tests/components/homekit/test_type_lights.py @@ -30,8 +30,7 @@ async def test_light_basic(hass, hk_driver, cls): """Test light with char state.""" entity_id = 'light.demo' - hass.states.async_set(entity_id, STATE_ON, - {ATTR_SUPPORTED_FEATURES: 0}) + hass.states.async_set(entity_id, STATE_ON, {ATTR_SUPPORTED_FEATURES: 0}) await hass.async_block_till_done() acc = cls.light(hass, hk_driver, 'Light', entity_id, 2, None) @@ -43,8 +42,7 @@ async def test_light_basic(hass, hk_driver, cls): await hass.async_block_till_done() assert acc.char_on.value == 1 - hass.states.async_set(entity_id, STATE_OFF, - {ATTR_SUPPORTED_FEATURES: 0}) + hass.states.async_set(entity_id, STATE_OFF, {ATTR_SUPPORTED_FEATURES: 0}) await hass.async_block_till_done() assert acc.char_on.value == 0 diff --git a/tests/components/homekit/test_type_media_players.py b/tests/components/homekit/test_type_media_players.py index 4076b1f8a89..681cbba7252 100644 --- a/tests/components/homekit/test_type_media_players.py +++ b/tests/components/homekit/test_type_media_players.py @@ -1,16 +1,14 @@ """Test different accessory types: Media Players.""" -from homeassistant.components.media_player import ( - ATTR_MEDIA_VOLUME_MUTED, DOMAIN) -from homeassistant.components.homekit.type_media_players import MediaPlayer from homeassistant.components.homekit.const import ( CONF_FEATURE_LIST, FEATURE_ON_OFF, FEATURE_PLAY_PAUSE, FEATURE_PLAY_STOP, FEATURE_TOGGLE_MUTE) +from homeassistant.components.homekit.type_media_players import MediaPlayer +from homeassistant.components.media_player import ( + ATTR_MEDIA_VOLUME_MUTED, DOMAIN) from homeassistant.const import ( - ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, SERVICE_MEDIA_PAUSE, - SERVICE_MEDIA_PLAY, SERVICE_MEDIA_STOP, SERVICE_TURN_OFF, SERVICE_TURN_ON, - SERVICE_VOLUME_MUTE, STATE_IDLE, STATE_OFF, STATE_ON, STATE_PAUSED, - STATE_PLAYING) + ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, STATE_IDLE, STATE_OFF, STATE_ON, + STATE_PAUSED, STATE_PLAYING) from tests.common import async_mock_service @@ -59,12 +57,12 @@ async def test_media_player_set_state(hass, hk_driver): assert acc.chars[FEATURE_PLAY_STOP].value == 0 # Set from HomeKit - call_turn_on = async_mock_service(hass, DOMAIN, SERVICE_TURN_ON) - call_turn_off = async_mock_service(hass, DOMAIN, SERVICE_TURN_OFF) - call_media_play = async_mock_service(hass, DOMAIN, SERVICE_MEDIA_PLAY) - call_media_pause = async_mock_service(hass, DOMAIN, SERVICE_MEDIA_PAUSE) - call_media_stop = async_mock_service(hass, DOMAIN, SERVICE_MEDIA_STOP) - call_toggle_mute = async_mock_service(hass, DOMAIN, SERVICE_VOLUME_MUTE) + call_turn_on = async_mock_service(hass, DOMAIN, 'turn_on') + call_turn_off = async_mock_service(hass, DOMAIN, 'turn_off') + call_media_play = async_mock_service(hass, DOMAIN, 'media_play') + call_media_pause = async_mock_service(hass, DOMAIN, 'media_pause') + call_media_stop = async_mock_service(hass, DOMAIN, 'media_stop') + call_toggle_mute = async_mock_service(hass, DOMAIN, 'volume_mute') await hass.async_add_job(acc.chars[FEATURE_ON_OFF] .client_update_value, True) diff --git a/tests/components/homekit/test_type_sensors.py b/tests/components/homekit/test_type_sensors.py index 54ecbcb196f..901a8e76856 100644 --- a/tests/components/homekit/test_type_sensors.py +++ b/tests/components/homekit/test_type_sensors.py @@ -14,8 +14,7 @@ async def test_temperature(hass, hk_driver): hass.states.async_set(entity_id, None) await hass.async_block_till_done() - acc = TemperatureSensor(hass, hk_driver, 'Temperature', - entity_id, 2, None) + acc = TemperatureSensor(hass, hk_driver, 'Temperature', entity_id, 2, None) await hass.async_add_job(acc.run) assert acc.aid == 2 @@ -70,8 +69,7 @@ async def test_air_quality(hass, hk_driver): hass.states.async_set(entity_id, None) await hass.async_block_till_done() - acc = AirQualitySensor(hass, hk_driver, 'Air Quality', - entity_id, 2, None) + acc = AirQualitySensor(hass, hk_driver, 'Air Quality', entity_id, 2, None) await hass.async_add_job(acc.run) assert acc.aid == 2 diff --git a/tests/components/homekit/test_type_switches.py b/tests/components/homekit/test_type_switches.py index 3a09d2715d1..c2b80226508 100644 --- a/tests/components/homekit/test_type_switches.py +++ b/tests/components/homekit/test_type_switches.py @@ -1,9 +1,9 @@ """Test different accessory types: Switches.""" import pytest -from homeassistant.core import split_entity_id from homeassistant.components.homekit.type_switches import Outlet, Switch from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON +from homeassistant.core import split_entity_id from tests.common import async_mock_service diff --git a/tests/components/homekit/test_type_thermostats.py b/tests/components/homekit/test_type_thermostats.py index 00e3e2d22fc..45c340e58c4 100644 --- a/tests/components/homekit/test_type_thermostats.py +++ b/tests/components/homekit/test_type_thermostats.py @@ -13,7 +13,7 @@ from homeassistant.components.homekit.const import ( PROP_MAX_VALUE, PROP_MIN_VALUE) from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, ATTR_UNIT_OF_MEASUREMENT, - STATE_OFF, TEMP_CELSIUS, TEMP_FAHRENHEIT) + CONF_TEMPERATURE_UNIT, STATE_OFF, TEMP_CELSIUS, TEMP_FAHRENHEIT) from tests.common import async_mock_service from tests.components.homekit.common import patch_debounce @@ -323,7 +323,7 @@ async def test_thermostat_fahrenheit(hass, hk_driver, cls): # support_auto = True hass.states.async_set(entity_id, STATE_OFF, {ATTR_SUPPORTED_FEATURES: 6}) await hass.async_block_till_done() - with patch.object(hass.config.units, 'temperature_unit', + with patch.object(hass.config.units, CONF_TEMPERATURE_UNIT, new=TEMP_FAHRENHEIT): acc = cls.thermostat(hass, hk_driver, 'Climate', entity_id, 2, None) await hass.async_add_job(acc.run) diff --git a/tests/components/homekit/test_util.py b/tests/components/homekit/test_util.py index fa9fddee5fc..9be92b817be 100644 --- a/tests/components/homekit/test_util.py +++ b/tests/components/homekit/test_util.py @@ -2,7 +2,6 @@ import pytest 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, TYPE_OUTLET) @@ -17,6 +16,7 @@ from homeassistant.components.persistent_notification import ( from homeassistant.const import ( ATTR_CODE, ATTR_SUPPORTED_FEATURES, CONF_NAME, CONF_TYPE, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT) +from homeassistant.core import State from tests.common import async_mock_service