Skip to content

Commit

Permalink
Scene generation options removed
Browse files Browse the repository at this point in the history
  • Loading branch information
JoDehli committed Dec 11, 2024
1 parent 178a33c commit f8ca3bc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 39 deletions.
71 changes: 57 additions & 14 deletions custom_components/loxone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
CMD_GET_VISUAL_PASSWD, CMD_KEY_EXCHANGE, CMD_REFRESH_TOKEN,
CMD_REFRESH_TOKEN_JSON_WEB, CMD_REQUEST_TOKEN,
CMD_REQUEST_TOKEN_JSON_WEB,
CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN, CONF_SCENE_GEN,
CONF_SCENE_GEN_DELAY, DEFAULT, DEFAULT_DELAY_SCENE,
CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN, DEFAULT,
DEFAULT_PORT, DEFAULT_TOKEN_PERSIST_NAME, DOMAIN,
DOMAIN_DEVICES, ERROR_VALUE, EVENT, IV_BYTES,
KEEP_ALIVE_PERIOD, LOXAPPPATH, LOXONE_PLATFORMS,
Expand All @@ -62,10 +61,6 @@
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_SCENE_GEN, default=True): cv.boolean,
vol.Optional(
CONF_SCENE_GEN_DELAY, default=DEFAULT_DELAY_SCENE
): cv.positive_int,
vol.Required(CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN, default=False): bool,
}
),
Expand Down Expand Up @@ -97,19 +92,67 @@ async def async_setup(hass, config):
return True


async def async_migrate_entry(hass, config_entry):
# _LOGGER.debug("Migrating from version %s", config_entry.version)
# https://github.com/home-assistant/core/blob/555d7f1ea420acb969194ab00d91e85626a368d9/homeassistant/components/intellifire/__init__.py#L63
# https://github.com/home-assistant/core/blob/17533823075d68068ca9cf69c90b12088a0a2eb8/homeassistant/components/stookwijzer/__init__.py#L39
# https://github.com/home-assistant/core/blob/555d7f1ea420acb969194ab00d91e85626a368d9/homeassistant/components/airvisual/__init__.py#L230
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
"""
Handle migration of configuration entries to newer versions.
Parameters:
hass: Home Assistant instance.
config_entry: The configuration entry to be migrated.
Returns:
True if the migration was successful.
"""
# Migration logic from version 1 to 2
if config_entry.version == 1:
new = {**config_entry.options, CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN: True}
config_entry.options = {**new}
config_entry.version = 2
# Add option for light controller subcontrol generation
new_options = {
**config_entry.options,
CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN: True,
}

# Update the entry with the new options and version
hass.config_entries.async_update_entry(
config_entry,
options=new_options,
version=2,
)
_LOGGER.info("Migration to version %s successful", 2)

# Migration logic from version 2 to 3
if config_entry.version == 2:
new = {**config_entry.options, CONF_SCENE_GEN_DELAY: DEFAULT_DELAY_SCENE}
config_entry.options = {**new}
config_entry.version = 3
# Add scene delay and generation
new_options = {
**config_entry.options,
CONF_SCENE_GEN_DELAY: DEFAULT_DELAY_SCENE,
}

# Update the entry with the new options and version
hass.config_entries.async_update_entry(
config_entry,
options=new_options,
version=3,
)
_LOGGER.info("Migration to version %s successful", 3)

# Migration logic from version 3 to 4
if config_entry.version == 3:
# Remove deprecated configuration options
new_options = {**config_entry.options}
new_options.pop(CONF_SCENE_GEN, None) # Remove if exists
new_options.pop(CONF_SCENE_GEN_DELAY, None) # Remove if exists

# Update the entry with the new options and version
hass.config_entries.async_update_entry(
config_entry,
options=new_options,
version=4,
)
_LOGGER.info("Migration to version %s successful", 4)

return True


Expand Down
13 changes: 2 additions & 11 deletions custom_components/loxone/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
CONF_USERNAME)
from homeassistant.core import callback

from .const import (CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN, CONF_SCENE_GEN,
CONF_SCENE_GEN_DELAY, DEFAULT_DELAY_SCENE, DEFAULT_IP,
from .const import (CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN, DEFAULT_IP,
DEFAULT_PORT, DOMAIN)

LOXONE_SCHEMA = vol.Schema(
Expand All @@ -23,8 +22,6 @@
vol.Required(CONF_PASSWORD, default=""): str,
vol.Required(CONF_HOST, default=DEFAULT_IP): str,
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
vol.Required(CONF_SCENE_GEN, default=True): bool,
vol.Optional(CONF_SCENE_GEN_DELAY, default=DEFAULT_DELAY_SCENE): int,
vol.Required(CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN, default=False): bool,
}
)
Expand All @@ -33,7 +30,7 @@
class LoxoneFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle Pyloxone handle."""

VERSION = 3
VERSION = 4
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL

@staticmethod
Expand Down Expand Up @@ -76,10 +73,6 @@ async def async_step_init(self, user_input=None):
password = self.config_entry.options.get(CONF_PASSWORD, "")
host = self.config_entry.options.get(CONF_HOST, "")
port = self.config_entry.options.get(CONF_PORT, 80)
gen_scenes = self.config_entry.options.get(CONF_SCENE_GEN, True)
gen_scene_delay = self.config_entry.options.get(
CONF_SCENE_GEN_DELAY, DEFAULT_DELAY_SCENE
)
gen_subcontrols = self.config_entry.options.get(
CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN, False
)
Expand All @@ -90,8 +83,6 @@ async def async_step_init(self, user_input=None):
options[vol.Required(CONF_PASSWORD, default=password)] = str
options[vol.Required(CONF_HOST, default=host)] = str
options[vol.Required(CONF_PORT, default=port)] = int
options[vol.Required(CONF_SCENE_GEN, default=gen_scenes)] = bool
options[vol.Required(CONF_SCENE_GEN_DELAY, default=gen_scene_delay)] = int
options[
vol.Required(CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN, default=gen_subcontrols)
] = bool
Expand Down
3 changes: 0 additions & 3 deletions custom_components/loxone/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
DEFAULT_TOKEN_PERSIST_NAME = "lox_token.cfg"
ERROR_VALUE = -1
DEFAULT_PORT = 8080
DEFAULT_DELAY_SCENE = 3
DEFAULT_IP = ""

EVENT = "loxone_event"
Expand All @@ -83,8 +82,6 @@
DOMAIN_DEVICES = "devices"

CONF_ACTIONID = "uuidAction"
CONF_SCENE_GEN = "generate_scenes"
CONF_SCENE_GEN_DELAY = "generate_scenes_delay"
CONF_LIGHTCONTROLLER_SUBCONTROLS_GEN = "generate_lightcontroller_subcontrols"
DEFAULT_FORCE_UPDATE = False

Expand Down
16 changes: 5 additions & 11 deletions custom_components/loxone/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
async_call_later)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from .const import (CONF_SCENE_GEN, CONF_SCENE_GEN_DELAY, DEFAULT_DELAY_SCENE,
DOMAIN, SENDDOMAIN)
from .const import DOMAIN, SENDDOMAIN

_LOGGER = logging.getLogger(__name__)

Expand All @@ -35,19 +34,15 @@ async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
) -> True:
"""Set up Scenes."""

delay_scene = config_entry.options.get(CONF_SCENE_GEN_DELAY, DEFAULT_DELAY_SCENE)
create_scene = config_entry.options.get(CONF_SCENE_GEN, False)

async def setup_scenes():
print("Generate Scenes")
_LOGGER.debug("Generate Scenes...")
scenes = []
entity_ids = hass.states.async_entity_ids("LIGHT")

for _ in entity_ids:
print("Entity:", _)
state = hass.states.get(_)
att = state.attributes
if "platform" in att and att["platform"] == DOMAIN:
Expand All @@ -66,9 +61,8 @@ async def setup_scenes():
)
async_add_entities(scenes)

if create_scene:
unsub = async_dispatcher_connect(hass, f"{DOMAIN}_light_ready", setup_scenes)
hass.data.setdefault(DOMAIN, {}).setdefault("unsub", []).append(unsub)
unsub = async_dispatcher_connect(hass, f"{DOMAIN}_light_ready", setup_scenes)
hass.data.setdefault(DOMAIN, {}).setdefault("unsub", []).append(unsub)

return True

Expand Down

0 comments on commit f8ca3bc

Please sign in to comment.