Skip to content

Commit

Permalink
Uptime counter (#436)
Browse files Browse the repository at this point in the history
- Added option to wait for minimum openHAB uptime (default 60s)
- Renamed mqtt.connection.client_id to identifier (backwards compatible)
- Updated dependencies
  • Loading branch information
spacemanspiff2007 authored Feb 1, 2024
1 parent eede9f6 commit 470b60d
Show file tree
Hide file tree
Showing 62 changed files with 637 additions and 172 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/publish-dockerhub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ jobs:
environment: release

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
ref: master

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
uses: docker/setup-qemu-action@v3
with:
platforms: all

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3
with:
version: latest

- name: Login to DockerHub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USER }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build and push release
id: docker_build_release
uses: docker/build-push-action@v4
uses: docker/build-push-action@v5
with:
push: true
platforms: linux/amd64,linux/arm64,linux/arm/v7
Expand All @@ -46,7 +46,7 @@ jobs:

- name: Build and push latest
id: docker_build_latest
uses: docker/build-push-action@v4
uses: docker/build-push-action@v5
with:
push: true
platforms: linux/amd64,linux/arm64,linux/arm/v7
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
id-token: write

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: master

- name: Set up Python 3.10
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.10'

Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/run-tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
name: pre-commit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
python-version: '3.11'
- uses: pre-commit/[email protected]


Expand All @@ -23,9 +23,9 @@ jobs:
python-version: ['3.8', '3.9', '3.10', '3.11']

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-ast
- id: check-builtin-literals
Expand All @@ -22,7 +22,7 @@ repos:


- repo: https://github.com/PyCQA/flake8
rev: '6.1.0'
rev: '7.0.0'
hooks:
- id: flake8
# additional_dependencies:
Expand Down
6 changes: 5 additions & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ src = ["src", "test"]
# https://docs.astral.sh/ruff/settings/#ignore-init-module-imports
ignore-init-module-imports = true

extend-exclude = ["__init__.py"]
extend-exclude = [
"__init__.py",
"src/__test_*.py"
]

select = [
"E", "W", # https://docs.astral.sh/ruff/rules/#pycodestyle-e-w
Expand Down Expand Up @@ -62,6 +65,7 @@ builtins-ignorelist = ["id", "input"]
[lint.per-file-ignores]
"docs/conf.py" = ["INP001", "A001"]
"setup.py" = ["PTH123"]
"run/**" = ["INP001"]


[lint.isort]
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from docutils.nodes import Node, Text
from sphinx.addnodes import desc_signature


IS_RTD_BUILD = os.environ.get('READTHEDOCS', '-').lower() == 'true'
IS_CI = os.environ.get('CI', '-') == 'true'

Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Example
mqtt:
connection:
client_id: HABApp
identifier: HABApp
host: ''
port: 8883
user: ''
Expand Down
4 changes: 2 additions & 2 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Packages required to build the documentation
sphinx == 7.2.6
sphinx-autodoc-typehints == 1.25.2
sphinx-autodoc-typehints == 1.25.3
sphinx_rtd_theme == 2.0.0
sphinx-exec-code == 0.10
sphinx-exec-code == 0.12
autodoc_pydantic == 2.0.1
sphinx-copybutton == 0.5.2
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ MyOpenhabRule()
```

# Changelog
#### 24.02.0 (2024-XX-XX)
- For openHAB >= 4.1 it's possible to wait for a minimum openHAB uptime before connecting (defaults to 60s)
- Renamed config entry mqtt.connection.client_id to identifier (backwards compatible)
- Updated dependencies

#### 24.01.0 (2024-01-08)
- Added HABApp.util.RateLimiter
- Added CompressedMidnightRotatingFileHandler
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Packages for source formatting
# -----------------------------------------------------------------------------
pre-commit == 3.5.0 # 3.6.0 requires python >= 3.10
ruff == 0.1.11
ruff == 0.1.15

# -----------------------------------------------------------------------------
# Packages for other developement tasks
Expand Down
10 changes: 5 additions & 5 deletions requirements_setup.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
aiohttp == 3.9.1
pydantic == 2.5.3
msgspec == 0.18.5
aiohttp == 3.9.3
pydantic == 2.6.0
msgspec == 0.18.6
bidict == 0.22.1
watchdog == 3.0.0
ujson == 5.9.0
aiomqtt == 1.2.1
aiomqtt == 2.0.0

immutables == 0.20
eascheduler == 0.1.11
easyconfig == 0.3.1
easyconfig == 0.3.2
pendulum == 2.1.2
stack_data == 0.6.3
colorama == 0.4.6
Expand Down
2 changes: 1 addition & 1 deletion requirements_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
# -----------------------------------------------------------------------------
packaging == 23.2
pytest == 7.4.4
pytest-asyncio == 0.23.3
pytest-asyncio == 0.23.4
2 changes: 1 addition & 1 deletion run/conf/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ location:

mqtt:
connection:
client_id: HABAppConf
identifier: HABAppConf
host: localhost
password: ''
port: 1883
Expand Down
1 change: 1 addition & 0 deletions run/conf/rules/logging_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import HABApp


log = logging.getLogger('MyRule')


Expand Down
6 changes: 3 additions & 3 deletions run/conf/rules/openhab_rule.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import HABApp
from HABApp.core.events import ValueUpdateEvent, ValueChangeEvent
from HABApp.openhab.events import ItemStateEvent, ItemCommandEvent, ItemStateChangedEvent
from HABApp.openhab.items import SwitchItem, ContactItem, DatetimeItem
from HABApp.core.events import ValueChangeEvent, ValueUpdateEvent
from HABApp.openhab.events import ItemCommandEvent, ItemStateChangedEvent, ItemStateEvent
from HABApp.openhab.items import ContactItem, DatetimeItem, SwitchItem


class MyOpenhabRule(HABApp.Rule):
Expand Down
2 changes: 1 addition & 1 deletion run/conf/rules/openhab_things.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from HABApp import Rule
from HABApp.core.events import EventFilter
from HABApp.openhab.events import ThingStatusInfoChangedEvent
from HABApp.openhab.items import Thing
from HABApp.core.events import EventFilter


class CheckAllThings(Rule):
Expand Down
2 changes: 1 addition & 1 deletion run/conf/rules/openhab_to_mqtt_rule.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import HABApp
from HABApp.openhab.events import ItemStateUpdatedEventFilter, ItemStateEvent
from HABApp.openhab.events import ItemStateEvent, ItemStateUpdatedEventFilter
from HABApp.openhab.items import OpenhabItem


Expand Down
3 changes: 2 additions & 1 deletion run/conf/rules/time_rule.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import time, timedelta, datetime
from datetime import datetime, time, timedelta

from HABApp import Rule


Expand Down
2 changes: 1 addition & 1 deletion run/conf_listen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ location:

mqtt:
connection:
client_id: HABAppTesting
identifier: HABAppTesting
host: localhost
user: ''
password: ''
Expand Down
2 changes: 1 addition & 1 deletion run/conf_testing/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ location:

mqtt:
connection:
client_id: HABAppTesting
identifier: HABAppTesting
host: 'localhost'
port: 1883
user: ''
Expand Down
18 changes: 12 additions & 6 deletions run/conf_testing/lib/HABAppTests/event_waiter.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import logging
import time
from typing import TypeVar, Dict, Any
from typing import Union
from typing import Any, Dict, TypeVar, Union

from HABApp.core.events.filter import EventFilter
from HABApp.core.internals import EventBusListener, wrap_func, EventFilterBase, HINT_EVENT_FILTER_OBJ, \
get_current_context
from HABApp.core.internals import (
HINT_EVENT_FILTER_OBJ,
EventBusListener,
EventFilterBase,
get_current_context,
wrap_func,
)
from HABApp.core.items import BaseValueItem
from HABAppTests.errors import TestCaseFailed

from .compare_values import get_equal_text, get_value_text


log = logging.getLogger('HABApp.Tests')

EVENT_TYPE = TypeVar('EVENT_TYPE')
Expand Down Expand Up @@ -52,8 +58,8 @@ def wait_for_event(self, **kwargs) -> EVENT_TYPE:

if time.time() > start + self.timeout:
expected_values = "with " + ", ".join([f"{__k}={__v}" for __k, __v in kwargs.items()]) if kwargs else ""
raise TestCaseFailed(f'Timeout while waiting for {self.event_filter.describe()} '
f'for {self.name} {expected_values}')
msg = f'Timeout while waiting for {self.event_filter.describe()} for {self.name} {expected_values}'
raise TestCaseFailed(msg)

if not self._received_events:
continue
Expand Down
6 changes: 3 additions & 3 deletions run/conf_testing/lib/HABAppTests/item_waiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from HABAppTests.compare_values import get_equal_text
from HABAppTests.errors import TestCaseFailed


log = logging.getLogger('HABApp.Tests')


Expand Down Expand Up @@ -35,9 +36,8 @@ def wait_for_attribs(self, **kwargs):
for name, target in kwargs.items()
]
failed_msg = "\n".join(failed)
raise TestCaseFailed(f'Timeout waiting for {self.item.name}!\n{failed_msg}')

raise ValueError()
msg = f'Timeout waiting for {self.item.name}!\n{failed_msg}'
raise TestCaseFailed(msg)

def wait_for_state(self, state=None):
return self.wait_for_attribs(value=state)
Expand Down
14 changes: 8 additions & 6 deletions run/conf_testing/lib/HABAppTests/openhab_tmp_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

import HABApp
from HABApp.openhab.definitions.topics import TOPIC_ITEMS
from . import get_random_name, EventWaiter

from . import EventWaiter, get_random_name


class OpenhabTmpItem:
@staticmethod
def use(type: str, name: Optional[str] = None, arg_name: str = 'item'):
def use(item_type: str, name: Optional[str] = None, arg_name: str = 'item'):
def decorator(func):
@wraps(func)
def new_func(*args, **kwargs):
assert arg_name not in kwargs, f'arg {arg_name} already set'
item = OpenhabTmpItem(type, name)
item = OpenhabTmpItem(item_type, name)
try:
kwargs[arg_name] = item
return func(*args, **kwargs)
Expand All @@ -24,11 +25,11 @@ def new_func(*args, **kwargs):
return decorator

@staticmethod
def create(type: str, name: Optional[str] = None, arg_name: Optional[str] = None):
def create(item_type: str, name: Optional[str] = None, arg_name: Optional[str] = None):
def decorator(func):
@wraps(func)
def new_func(*args, **kwargs):
with OpenhabTmpItem(type, name) as f:
with OpenhabTmpItem(item_type, name) as f:
if arg_name is not None:
assert arg_name not in kwargs, f'arg {arg_name} already set'
kwargs[arg_name] = f
Expand Down Expand Up @@ -69,7 +70,8 @@ def create_item(self, label="", category="", tags: List[str] = [], groups: List[
while not HABApp.core.Items.item_exists(self.name):
time.sleep(0.01)
if time.time() > stop:
raise TimeoutError(f'Item {self.name} was not found!')
msg = f'Item {self.name} was not found!'
raise TimeoutError(msg)

return HABApp.openhab.items.OpenhabItem.get_item(self.name)

Expand Down
6 changes: 4 additions & 2 deletions run/conf_testing/rules/test_mqtt.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import logging
import time

from HABAppTests import EventWaiter, ItemWaiter, TestBaseRule, run_coro

import HABApp
from HABApp.core.connections import Connections, ConnectionStatus
from HABApp.core.events import ValueUpdateEventFilter
from HABApp.mqtt.events import MqttValueUpdateEventFilter
from HABApp.mqtt.items import MqttItem, MqttPairItem
from HABAppTests import EventWaiter, ItemWaiter, TestBaseRule, run_coro


log = logging.getLogger('HABApp.MqttTestEvents')

Expand Down Expand Up @@ -73,10 +75,10 @@ def test_mqtt_item_creation(self):

run_coro(self.trigger_reconnect())

time.sleep(0.2)
connection = Connections.get('mqtt')
while not connection.is_online:
time.sleep(0.2)

assert HABApp.core.Items.item_exists(topic) is True

HABApp.core.Items.pop_item(topic)
Expand Down
Loading

0 comments on commit 470b60d

Please sign in to comment.