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
This commit is contained in:
Patrik 2019-10-09 21:56:16 +02:00 committed by Paulus Schoutsen
parent a8db8d8c0b
commit 78e9bba279
8 changed files with 73 additions and 69 deletions

View file

@ -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

View file

@ -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

View file

@ -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."""

View file

@ -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"]

View file

@ -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):

View file

@ -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

View file

@ -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__)

View file

@ -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):