From b5f8a9646f9e32ca2a0f38ff4b405dc913af2814 Mon Sep 17 00:00:00 2001 From: Steve Herrell <47094394+twrecked@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:44:38 -0500 Subject: [PATCH] Alpha 05 (#33) * Fix import not handling time deltas. * Fix import not handling time deltas. --- changelog | 2 ++ custom_components/momentary/__init__.py | 2 +- custom_components/momentary/cfg.py | 13 +++++++++++++ custom_components/momentary/manifest.json | 2 +- custom_components/momentary/switch.py | 6 +++--- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/changelog b/changelog index 017efa9..05d6ad8 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,5 @@ +0.7.0a5: + fix timedelta on import 0.7.0a4: allow multiple switches per device 0.7.0a3: diff --git a/custom_components/momentary/__init__.py b/custom_components/momentary/__init__.py index 5643b96..1cac021 100644 --- a/custom_components/momentary/__init__.py +++ b/custom_components/momentary/__init__.py @@ -16,7 +16,7 @@ from .cfg import BlendedCfg -__version__ = '0.7.0a4' +__version__ = '0.7.0a5' _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/momentary/cfg.py b/custom_components/momentary/cfg.py index 077b5fb..7de087d 100644 --- a/custom_components/momentary/cfg.py +++ b/custom_components/momentary/cfg.py @@ -25,6 +25,14 @@ DB_LOCK = threading.Lock() +def _fix_value(value): + """ If needed, convert value into a type that can be stored in yaml. + """ + if isinstance(value, timedelta): + return max(value.seconds, 1) + return value + + def _load_meta_data(group_name: str): """Read in meta data for a particular group. """ @@ -379,6 +387,8 @@ def import_switch(self, switch): """ Import an original YAML entry. """ + _LOGGER.debug(f"import={switch}") + # We remove the platform field. We deepcopy before doing this to # quiet down some startup errors. switch = copy.deepcopy(switch) @@ -394,6 +404,9 @@ def import_switch(self, switch): switch[ATTR_NAME] = _map_config_name(switch[ATTR_NAME]) device_id = _make_device_id(switch[ATTR_NAME]) + # Fix time deltas + switch = {k: _fix_value(v) for k, v in switch.items()} + # Create switch data and meta data. self._switches.update({ unique_id: switch diff --git a/custom_components/momentary/manifest.json b/custom_components/momentary/manifest.json index 1f64b61..f96f75f 100644 --- a/custom_components/momentary/manifest.json +++ b/custom_components/momentary/manifest.json @@ -8,5 +8,5 @@ "iot_class": "local_polling", "issue_tracker": "https://github.com/twrecked/hass-momentary/issues", "requirements": [], - "version": "0.7.0a4" + "version": "0.7.0a5" } diff --git a/custom_components/momentary/switch.py b/custom_components/momentary/switch.py index 17109c6..9e28d30 100644 --- a/custom_components/momentary/switch.py +++ b/custom_components/momentary/switch.py @@ -5,8 +5,8 @@ import logging import pprint import voluptuous as vol -from collections.abc import Callable from datetime import datetime +from collections.abc import Callable from typing import Any import homeassistant.helpers.config_validation as cv @@ -94,8 +94,6 @@ def __init__(self, unique_id, config, hass): self._timed_state = False _LOGGER.debug(f'new config, idle-state={self._idle_state}') - _LOGGER.info(f'MomentarySwitch: {self.name} created') - # Home Assistant stuff. self.entity_id = config[ATTR_ENTITY_ID] self._attr_name = config.get(CONF_NAME) @@ -106,6 +104,8 @@ def __init__(self, unique_id, config, hass): model=COMPONENT_MODEL, ) + _LOGGER.info(f'MomentarySwitch: {self._attr_name} created') + def _create_state(self): _LOGGER.info(f'Momentary {self.unique_id}: creating initial state') self._attr_is_on = self._idle_state