Strict creation of the config for dynalite (#34663)
This commit is contained in:
parent
34e35f6aa3
commit
710deb8316
3 changed files with 117 additions and 37 deletions
|
@ -32,47 +32,128 @@ from .const import (
|
||||||
CONF_TIME_COVER,
|
CONF_TIME_COVER,
|
||||||
)
|
)
|
||||||
|
|
||||||
CONF_MAP = {
|
ACTIVE_MAP = {
|
||||||
CONF_ACTIVE: dyn_const.CONF_ACTIVE,
|
|
||||||
ACTIVE_INIT: dyn_const.ACTIVE_INIT,
|
ACTIVE_INIT: dyn_const.ACTIVE_INIT,
|
||||||
|
False: dyn_const.ACTIVE_OFF,
|
||||||
ACTIVE_OFF: dyn_const.ACTIVE_OFF,
|
ACTIVE_OFF: dyn_const.ACTIVE_OFF,
|
||||||
ACTIVE_ON: dyn_const.ACTIVE_ON,
|
ACTIVE_ON: dyn_const.ACTIVE_ON,
|
||||||
CONF_AREA: dyn_const.CONF_AREA,
|
True: dyn_const.ACTIVE_ON,
|
||||||
CONF_AUTO_DISCOVER: dyn_const.CONF_AUTO_DISCOVER,
|
}
|
||||||
CONF_CHANNEL: dyn_const.CONF_CHANNEL,
|
|
||||||
CONF_CHANNEL_COVER: dyn_const.CONF_CHANNEL_COVER,
|
TEMPLATE_MAP = {
|
||||||
CONF_TYPE: dyn_const.CONF_CHANNEL_TYPE,
|
|
||||||
CONF_CLOSE_PRESET: dyn_const.CONF_CLOSE_PRESET,
|
|
||||||
CONF_DEFAULT: dyn_const.CONF_DEFAULT,
|
|
||||||
CONF_DEVICE_CLASS: dyn_const.CONF_DEVICE_CLASS,
|
|
||||||
CONF_DURATION: dyn_const.CONF_DURATION,
|
|
||||||
CONF_FADE: dyn_const.CONF_FADE,
|
|
||||||
CONF_HOST: dyn_const.CONF_HOST,
|
|
||||||
CONF_NAME: dyn_const.CONF_NAME,
|
|
||||||
CONF_NO_DEFAULT: dyn_const.CONF_NO_DEFAULT,
|
|
||||||
CONF_OPEN_PRESET: dyn_const.CONF_OPEN_PRESET,
|
|
||||||
CONF_POLL_TIMER: dyn_const.CONF_POLL_TIMER,
|
|
||||||
CONF_PORT: dyn_const.CONF_PORT,
|
|
||||||
CONF_PRESET: dyn_const.CONF_PRESET,
|
|
||||||
CONF_ROOM: dyn_const.CONF_ROOM,
|
CONF_ROOM: dyn_const.CONF_ROOM,
|
||||||
CONF_ROOM_OFF: dyn_const.CONF_ROOM_OFF,
|
|
||||||
CONF_ROOM_ON: dyn_const.CONF_ROOM_ON,
|
|
||||||
CONF_STOP_PRESET: dyn_const.CONF_STOP_PRESET,
|
|
||||||
CONF_TEMPLATE: dyn_const.CONF_TEMPLATE,
|
|
||||||
CONF_TILT_TIME: dyn_const.CONF_TILT_TIME,
|
|
||||||
CONF_TIME_COVER: dyn_const.CONF_TIME_COVER,
|
CONF_TIME_COVER: dyn_const.CONF_TIME_COVER,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def convert_with_map(config, conf_map):
|
||||||
|
"""Create the initial converted map with just the basic key:value pairs updated."""
|
||||||
|
result = {}
|
||||||
|
for conf in conf_map:
|
||||||
|
if conf in config:
|
||||||
|
result[conf_map[conf]] = config[conf]
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def convert_channel(config: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Convert the config for a channel."""
|
||||||
|
my_map = {
|
||||||
|
CONF_NAME: dyn_const.CONF_NAME,
|
||||||
|
CONF_FADE: dyn_const.CONF_FADE,
|
||||||
|
CONF_TYPE: dyn_const.CONF_CHANNEL_TYPE,
|
||||||
|
}
|
||||||
|
return convert_with_map(config, my_map)
|
||||||
|
|
||||||
|
|
||||||
|
def convert_preset(config: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Convert the config for a preset."""
|
||||||
|
my_map = {
|
||||||
|
CONF_NAME: dyn_const.CONF_NAME,
|
||||||
|
CONF_FADE: dyn_const.CONF_FADE,
|
||||||
|
}
|
||||||
|
return convert_with_map(config, my_map)
|
||||||
|
|
||||||
|
|
||||||
|
def convert_area(config: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Convert the config for an area."""
|
||||||
|
my_map = {
|
||||||
|
CONF_NAME: dyn_const.CONF_NAME,
|
||||||
|
CONF_FADE: dyn_const.CONF_FADE,
|
||||||
|
CONF_NO_DEFAULT: dyn_const.CONF_NO_DEFAULT,
|
||||||
|
CONF_ROOM_ON: dyn_const.CONF_ROOM_ON,
|
||||||
|
CONF_ROOM_OFF: dyn_const.CONF_ROOM_OFF,
|
||||||
|
CONF_CHANNEL_COVER: dyn_const.CONF_CHANNEL_COVER,
|
||||||
|
CONF_DEVICE_CLASS: dyn_const.CONF_DEVICE_CLASS,
|
||||||
|
CONF_OPEN_PRESET: dyn_const.CONF_OPEN_PRESET,
|
||||||
|
CONF_CLOSE_PRESET: dyn_const.CONF_CLOSE_PRESET,
|
||||||
|
CONF_STOP_PRESET: dyn_const.CONF_STOP_PRESET,
|
||||||
|
CONF_DURATION: dyn_const.CONF_DURATION,
|
||||||
|
CONF_TILT_TIME: dyn_const.CONF_TILT_TIME,
|
||||||
|
}
|
||||||
|
result = convert_with_map(config, my_map)
|
||||||
|
if CONF_CHANNEL in config:
|
||||||
|
result[dyn_const.CONF_CHANNEL] = {
|
||||||
|
channel: convert_channel(channel_conf)
|
||||||
|
for (channel, channel_conf) in config[CONF_CHANNEL].items()
|
||||||
|
}
|
||||||
|
if CONF_PRESET in config:
|
||||||
|
result[dyn_const.CONF_PRESET] = {
|
||||||
|
preset: convert_preset(preset_conf)
|
||||||
|
for (preset, preset_conf) in config[CONF_PRESET].items()
|
||||||
|
}
|
||||||
|
if CONF_TEMPLATE in config:
|
||||||
|
result[dyn_const.CONF_TEMPLATE] = TEMPLATE_MAP[config[CONF_TEMPLATE]]
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def convert_default(config: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Convert the config for the platform defaults."""
|
||||||
|
return convert_with_map(config, {CONF_FADE: dyn_const.CONF_FADE})
|
||||||
|
|
||||||
|
|
||||||
|
def convert_template(config: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Convert the config for a template."""
|
||||||
|
my_map = {
|
||||||
|
CONF_ROOM_ON: dyn_const.CONF_ROOM_ON,
|
||||||
|
CONF_ROOM_OFF: dyn_const.CONF_ROOM_OFF,
|
||||||
|
CONF_CHANNEL_COVER: dyn_const.CONF_CHANNEL_COVER,
|
||||||
|
CONF_DEVICE_CLASS: dyn_const.CONF_DEVICE_CLASS,
|
||||||
|
CONF_OPEN_PRESET: dyn_const.CONF_OPEN_PRESET,
|
||||||
|
CONF_CLOSE_PRESET: dyn_const.CONF_CLOSE_PRESET,
|
||||||
|
CONF_STOP_PRESET: dyn_const.CONF_STOP_PRESET,
|
||||||
|
CONF_DURATION: dyn_const.CONF_DURATION,
|
||||||
|
CONF_TILT_TIME: dyn_const.CONF_TILT_TIME,
|
||||||
|
}
|
||||||
|
return convert_with_map(config, my_map)
|
||||||
|
|
||||||
|
|
||||||
def convert_config(config: Dict[str, Any]) -> Dict[str, Any]:
|
def convert_config(config: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
"""Convert a config dict by replacing component consts with library consts."""
|
"""Convert a config dict by replacing component consts with library consts."""
|
||||||
result = {}
|
my_map = {
|
||||||
for (key, value) in config.items():
|
CONF_NAME: dyn_const.CONF_NAME,
|
||||||
if isinstance(value, dict):
|
CONF_HOST: dyn_const.CONF_HOST,
|
||||||
new_value = convert_config(value)
|
CONF_PORT: dyn_const.CONF_PORT,
|
||||||
elif isinstance(value, str):
|
CONF_AUTO_DISCOVER: dyn_const.CONF_AUTO_DISCOVER,
|
||||||
new_value = CONF_MAP.get(value, value)
|
CONF_POLL_TIMER: dyn_const.CONF_POLL_TIMER,
|
||||||
else:
|
}
|
||||||
new_value = value
|
result = convert_with_map(config, my_map)
|
||||||
result[CONF_MAP.get(key, key)] = new_value
|
if CONF_AREA in config:
|
||||||
|
result[dyn_const.CONF_AREA] = {
|
||||||
|
area: convert_area(area_conf)
|
||||||
|
for (area, area_conf) in config[CONF_AREA].items()
|
||||||
|
}
|
||||||
|
if CONF_DEFAULT in config:
|
||||||
|
result[dyn_const.CONF_DEFAULT] = convert_default(config[CONF_DEFAULT])
|
||||||
|
if CONF_ACTIVE in config:
|
||||||
|
result[dyn_const.CONF_ACTIVE] = ACTIVE_MAP[config[CONF_ACTIVE]]
|
||||||
|
if CONF_PRESET in config:
|
||||||
|
result[dyn_const.CONF_PRESET] = {
|
||||||
|
preset: convert_preset(preset_conf)
|
||||||
|
for (preset, preset_conf) in config[CONF_PRESET].items()
|
||||||
|
}
|
||||||
|
if CONF_TEMPLATE in config:
|
||||||
|
result[dyn_const.CONF_TEMPLATE] = {
|
||||||
|
TEMPLATE_MAP[template]: convert_template(template_conf)
|
||||||
|
for (template, template_conf) in config[CONF_TEMPLATE].items()
|
||||||
|
}
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -5,7 +5,6 @@ from homeassistant.components.cover import DEVICE_CLASSES, CoverEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
|
||||||
from .const import DEFAULT_COVER_CLASS
|
|
||||||
from .dynalitebase import DynaliteBase, async_setup_entry_base
|
from .dynalitebase import DynaliteBase, async_setup_entry_base
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,9 +31,8 @@ class DynaliteCover(DynaliteBase, CoverEntity):
|
||||||
def device_class(self) -> str:
|
def device_class(self) -> str:
|
||||||
"""Return the class of the device."""
|
"""Return the class of the device."""
|
||||||
dev_cls = self._device.device_class
|
dev_cls = self._device.device_class
|
||||||
if dev_cls in DEVICE_CLASSES:
|
assert dev_cls in DEVICE_CLASSES
|
||||||
return dev_cls
|
return dev_cls
|
||||||
return DEFAULT_COVER_CLASS
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_cover_position(self) -> int:
|
def current_cover_position(self) -> int:
|
||||||
|
|
|
@ -37,6 +37,7 @@ async def test_async_setup(hass):
|
||||||
"1": {
|
"1": {
|
||||||
CONF_NAME: "Name1",
|
CONF_NAME: "Name1",
|
||||||
dynalite.CONF_CHANNEL: {"4": {}},
|
dynalite.CONF_CHANNEL: {"4": {}},
|
||||||
|
dynalite.CONF_PRESET: {"7": {}},
|
||||||
dynalite.CONF_NO_DEFAULT: True,
|
dynalite.CONF_NO_DEFAULT: True,
|
||||||
},
|
},
|
||||||
"2": {CONF_NAME: "Name2"},
|
"2": {CONF_NAME: "Name2"},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue