From 78e9bba279878a581a494da534617aebaae61f90 Mon Sep 17 00:00:00 2001 From: Patrik <21142447+ggravlingen@users.noreply.github.com> Date: Wed, 9 Oct 2019 21:56:16 +0200 Subject: [PATCH] Refactor Tradfri constants (#27334) * Refactor constants * Rename constant * Rename constant * Rename constant * Review update * Remove duplicate constant * Reorder constants * Dont refresh features * Order package imports * Fix bug * Put back features in refresh * Fix import order * Refactor supported features * Refactor supported features, take 2 --- homeassistant/components/tradfri/__init__.py | 46 ++++++++---------- .../components/tradfri/base_class.py | 6 +-- .../components/tradfri/config_flow.py | 5 +- homeassistant/components/tradfri/const.py | 22 ++++++++- homeassistant/components/tradfri/cover.py | 7 ++- homeassistant/components/tradfri/light.py | 48 +++++++++---------- homeassistant/components/tradfri/sensor.py | 5 +- homeassistant/components/tradfri/switch.py | 3 +- 8 files changed, 73 insertions(+), 69 deletions(-) diff --git a/homeassistant/components/tradfri/__init__.py b/homeassistant/components/tradfri/__init__.py index bca91134bed..c719fa41614 100644 --- a/homeassistant/components/tradfri/__init__.py +++ b/homeassistant/components/tradfri/__init__.py @@ -3,12 +3,22 @@ import logging import voluptuous as vol +import homeassistant.helpers.config_validation as cv from homeassistant import config_entries from homeassistant.const import EVENT_HOMEASSISTANT_STOP -import homeassistant.helpers.config_validation as cv from homeassistant.util.json import load_json - +from . import config_flow # noqa pylint_disable=unused-import from .const import ( + DOMAIN, + CONFIG_FILE, + KEY_GATEWAY, + KEY_API, + CONF_ALLOW_TRADFRI_GROUPS, + DEFAULT_ALLOW_TRADFRI_GROUPS, + TRADFRI_DEVICE_TYPES, + ATTR_TRADFRI_MANUFACTURER, + ATTR_TRADFRI_GATEWAY, + ATTR_TRADFRI_GATEWAY_MODEL, CONF_IMPORT_GROUPS, CONF_IDENTITY, CONF_HOST, @@ -16,18 +26,8 @@ from .const import ( CONF_GATEWAY_ID, ) -from . import config_flow # noqa pylint_disable=unused-import - _LOGGER = logging.getLogger(__name__) - -DOMAIN = "tradfri" -CONFIG_FILE = ".tradfri_psk.conf" -KEY_GATEWAY = "tradfri_gateway" -KEY_API = "tradfri_api" -CONF_ALLOW_TRADFRI_GROUPS = "allow_tradfri_groups" -DEFAULT_ALLOW_TRADFRI_GROUPS = False - CONFIG_SCHEMA = vol.Schema( { DOMAIN: vol.Schema( @@ -124,24 +124,16 @@ async def async_setup_entry(hass, entry): config_entry_id=entry.entry_id, connections=set(), identifiers={(DOMAIN, entry.data[CONF_GATEWAY_ID])}, - manufacturer="IKEA", - name="Gateway", + manufacturer=ATTR_TRADFRI_MANUFACTURER, + name=ATTR_TRADFRI_GATEWAY, # They just have 1 gateway model. Type is not exposed yet. - model="E1526", + model=ATTR_TRADFRI_GATEWAY_MODEL, sw_version=gateway_info.firmware_version, ) - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "cover") - ) - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "light") - ) - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "sensor") - ) - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "switch") - ) + for device in TRADFRI_DEVICE_TYPES: + hass.async_create_task( + hass.config_entries.async_forward_entry_setup(entry, device) + ) return True diff --git a/homeassistant/components/tradfri/base_class.py b/homeassistant/components/tradfri/base_class.py index aa8487b087e..8430a342c09 100644 --- a/homeassistant/components/tradfri/base_class.py +++ b/homeassistant/components/tradfri/base_class.py @@ -5,7 +5,7 @@ from pytradfri.error import PytradfriError from homeassistant.core import callback from homeassistant.helpers.entity import Entity -from . import DOMAIN as TRADFRI_DOMAIN +from .const import DOMAIN _LOGGER = logging.getLogger(__name__) @@ -60,12 +60,12 @@ class TradfriBaseDevice(Entity): info = self._device.device_info return { - "identifiers": {(TRADFRI_DOMAIN, self._device.id)}, + "identifiers": {(DOMAIN, self._device.id)}, "manufacturer": info.manufacturer, "model": info.model_number, "name": self._name, "sw_version": info.firmware_version, - "via_device": (TRADFRI_DOMAIN, self._gateway_id), + "via_device": (DOMAIN, self._gateway_id), } @property diff --git a/homeassistant/components/tradfri/config_flow.py b/homeassistant/components/tradfri/config_flow.py index 9da381deb75..bdb195cf53f 100644 --- a/homeassistant/components/tradfri/config_flow.py +++ b/homeassistant/components/tradfri/config_flow.py @@ -7,18 +7,15 @@ import async_timeout import voluptuous as vol from homeassistant import config_entries - from .const import ( CONF_IMPORT_GROUPS, CONF_IDENTITY, CONF_HOST, CONF_KEY, CONF_GATEWAY_ID, + KEY_SECURITY_CODE, ) -KEY_SECURITY_CODE = "security_code" -KEY_IMPORT_GROUPS = "import_groups" - class AuthError(Exception): """Exception if authentication occurs.""" diff --git a/homeassistant/components/tradfri/const.py b/homeassistant/components/tradfri/const.py index d37b5d99f9f..a7acfcbf876 100644 --- a/homeassistant/components/tradfri/const.py +++ b/homeassistant/components/tradfri/const.py @@ -1,7 +1,25 @@ """Consts used by Tradfri.""" +from homeassistant.components.light import SUPPORT_TRANSITION, SUPPORT_BRIGHTNESS from homeassistant.const import CONF_HOST # noqa pylint: disable=unused-import -CONF_IMPORT_GROUPS = "import_groups" +ATTR_DIMMER = "dimmer" +ATTR_HUE = "hue" +ATTR_SAT = "saturation" +ATTR_TRADFRI_GATEWAY = "Gateway" +ATTR_TRADFRI_GATEWAY_MODEL = "E1526" +ATTR_TRADFRI_MANUFACTURER = "IKEA" +ATTR_TRANSITION_TIME = "transition_time" +CONF_ALLOW_TRADFRI_GROUPS = "allow_tradfri_groups" CONF_IDENTITY = "identity" -CONF_KEY = "key" +CONF_IMPORT_GROUPS = "import_groups" CONF_GATEWAY_ID = "gateway_id" +CONF_KEY = "key" +CONFIG_FILE = ".tradfri_psk.conf" +DEFAULT_ALLOW_TRADFRI_GROUPS = False +DOMAIN = "tradfri" +KEY_API = "tradfri_api" +KEY_GATEWAY = "tradfri_gateway" +KEY_SECURITY_CODE = "security_code" +SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION +SUPPORTED_LIGHT_FEATURES = SUPPORT_TRANSITION +TRADFRI_DEVICE_TYPES = ["cover", "light", "sensor", "switch"] diff --git a/homeassistant/components/tradfri/cover.py b/homeassistant/components/tradfri/cover.py index 3dea978044f..1a3bf841665 100644 --- a/homeassistant/components/tradfri/cover.py +++ b/homeassistant/components/tradfri/cover.py @@ -11,8 +11,7 @@ from homeassistant.components.cover import ( SUPPORT_SET_POSITION, ) from homeassistant.core import callback -from . import DOMAIN as TRADFRI_DOMAIN, KEY_API, KEY_GATEWAY -from .const import CONF_GATEWAY_ID +from .const import DOMAIN, KEY_GATEWAY, KEY_API, CONF_GATEWAY_ID _LOGGER = logging.getLogger(__name__) @@ -62,12 +61,12 @@ class TradfriCover(CoverDevice): info = self._cover.device_info return { - "identifiers": {(TRADFRI_DOMAIN, self._cover.id)}, + "identifiers": {(DOMAIN, self._cover.id)}, "name": self._name, "manufacturer": info.manufacturer, "model": info.model_number, "sw_version": info.firmware_version, - "via_device": (TRADFRI_DOMAIN, self._gateway_id), + "via_device": (DOMAIN, self._gateway_id), } async def async_added_to_hass(self): diff --git a/homeassistant/components/tradfri/light.py b/homeassistant/components/tradfri/light.py index f5d61f0aaed..089f80223e8 100644 --- a/homeassistant/components/tradfri/light.py +++ b/homeassistant/components/tradfri/light.py @@ -9,29 +9,28 @@ from homeassistant.components.light import ( ATTR_COLOR_TEMP, ATTR_HS_COLOR, ATTR_TRANSITION, - PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA, + Light, SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP, - SUPPORT_TRANSITION, - Light, ) -from homeassistant.components.tradfri.base_class import TradfriBaseDevice from homeassistant.core import callback -from . import KEY_API, KEY_GATEWAY -from .const import CONF_GATEWAY_ID, CONF_IMPORT_GROUPS +from .base_class import TradfriBaseDevice +from .const import ( + ATTR_DIMMER, + ATTR_HUE, + ATTR_SAT, + ATTR_TRANSITION_TIME, + SUPPORTED_LIGHT_FEATURES, + SUPPORTED_GROUP_FEATURES, + CONF_GATEWAY_ID, + CONF_IMPORT_GROUPS, + KEY_GATEWAY, + KEY_API, +) _LOGGER = logging.getLogger(__name__) -ATTR_DIMMER = "dimmer" -ATTR_HUE = "hue" -ATTR_SAT = "saturation" -ATTR_TRANSITION_TIME = "transition_time" -PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA -TRADFRI_LIGHT_MANAGER = "Tradfri Light Manager" -SUPPORTED_FEATURES = SUPPORT_TRANSITION -SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION - async def async_setup_entry(hass, config_entry, async_add_entities): """Load Tradfri lights based on a config entry.""" @@ -152,7 +151,16 @@ class TradfriLight(TradfriBaseDevice, Light): super().__init__(device, api, gateway_id) self._unique_id = f"light-{gateway_id}-{device.id}" self._hs_color = None - self._features = SUPPORTED_FEATURES + + # Calculate supported features + _features = SUPPORTED_LIGHT_FEATURES + if device.light_control.can_set_dimmer: + _features |= SUPPORT_BRIGHTNESS + if device.light_control.can_set_color: + _features |= SUPPORT_COLOR + if device.light_control.can_set_temp: + _features |= SUPPORT_COLOR_TEMP + self._features = _features self._refresh(device) @@ -297,11 +305,3 @@ class TradfriLight(TradfriBaseDevice, Light): # Caching of LightControl and light object self._device_control = device.light_control self._device_data = device.light_control.lights[0] - self._features = SUPPORTED_FEATURES - - if device.light_control.can_set_dimmer: - self._features |= SUPPORT_BRIGHTNESS - if device.light_control.can_set_color: - self._features |= SUPPORT_COLOR - if device.light_control.can_set_temp: - self._features |= SUPPORT_COLOR_TEMP diff --git a/homeassistant/components/tradfri/sensor.py b/homeassistant/components/tradfri/sensor.py index 7814daf8f7a..56c1a464580 100644 --- a/homeassistant/components/tradfri/sensor.py +++ b/homeassistant/components/tradfri/sensor.py @@ -1,10 +1,9 @@ """Support for IKEA Tradfri sensors.""" import logging -from homeassistant.components.tradfri.base_class import TradfriBaseDevice from homeassistant.const import DEVICE_CLASS_BATTERY -from . import KEY_API, KEY_GATEWAY -from .const import CONF_GATEWAY_ID +from .base_class import TradfriBaseDevice +from .const import KEY_GATEWAY, KEY_API, CONF_GATEWAY_ID _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/tradfri/switch.py b/homeassistant/components/tradfri/switch.py index 1e322ff47f5..e1c549a1805 100644 --- a/homeassistant/components/tradfri/switch.py +++ b/homeassistant/components/tradfri/switch.py @@ -1,8 +1,7 @@ """Support for IKEA Tradfri switches.""" from homeassistant.components.switch import SwitchDevice -from . import KEY_API, KEY_GATEWAY from .base_class import TradfriBaseDevice -from .const import CONF_GATEWAY_ID +from .const import KEY_GATEWAY, KEY_API, CONF_GATEWAY_ID async def async_setup_entry(hass, config_entry, async_add_entities):