Skip to content

Commit

Permalink
Alpha 05 (#33)
Browse files Browse the repository at this point in the history
* Fix import not handling time deltas.

* Fix import not handling time deltas.
  • Loading branch information
twrecked authored Nov 27, 2023
1 parent 4d8b3c9 commit b5f8a96
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.7.0a5:
fix timedelta on import
0.7.0a4:
allow multiple switches per device
0.7.0a3:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/momentary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .cfg import BlendedCfg


__version__ = '0.7.0a4'
__version__ = '0.7.0a5'

_LOGGER = logging.getLogger(__name__)

Expand Down
13 changes: 13 additions & 0 deletions custom_components/momentary/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion custom_components/momentary/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
6 changes: 3 additions & 3 deletions custom_components/momentary/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit b5f8a96

Please sign in to comment.