Skip to content

Commit

Permalink
Added ValueCommandEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Oct 17, 2024
1 parent 2fbb7bb commit 534acc7
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 41 deletions.
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.6.0
rev: v5.0.0
hooks:
- id: check-ast
- id: check-builtin-literals
Expand All @@ -15,7 +15,7 @@ repos:


- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.8
rev: v0.6.9
hooks:
- id: ruff
name: ruff unused imports
Expand Down
28 changes: 15 additions & 13 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@

line-length = 120
indent-width = 4
line-length = 120

target-version = "py310"
src = ["src", "test"]


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


[lint]
select = ["ALL"]
Expand Down Expand Up @@ -65,29 +64,36 @@ ignore = [


[format]
# Use single quotes for non-triple-quoted strings.
quote-style = "single"


# https://docs.astral.sh/ruff/settings/#lintflake8-quotes
[lint.flake8-quotes]
inline-quotes = "single"
inline-quotes = "single"
multiline-quotes = "single"


[lint.flake8-builtins]
builtins-ignorelist = ["id", "input"]


# https://docs.astral.sh/ruff/settings/#lintisort
[lint.isort]
lines-after-imports = 2 # https://docs.astral.sh/ruff/settings/#lint_isort_lines-after-imports


[lint.per-file-ignores]
"docs/conf.py" = [
"INP001", # File `conf.py` is part of an implicit namespace package. Add an `__init__.py`.
"A001", # Variable `copyright` is shadowing a Python builtin
"PTH118", # `os.path.join()` should be replaced by `Path` with `/` operator
"PTH100", # `os.path.abspath()` should be replaced by `Path.resolve()`
]

"setup.py" = ["PTH123"]

"run/**" = ["INP001"]

"tests/*" = [
"ANN", # https://docs.astral.sh/ruff/rules/#flake8-annotations-ann

Expand All @@ -102,7 +108,3 @@ builtins-ignorelist = ["id", "input"]
"interface_*.py" = [
"F401" # F401 [*] {name} imported but unused
]

[lint.isort]
# https://docs.astral.sh/ruff/settings/#isort-lines-after-imports
lines-after-imports = 2
15 changes: 15 additions & 0 deletions docs/interface_habapp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ This event gets emitted every time a value of an item changes
:inherited-members:


ValueCommandEvent
======================================

This event indicates that the item should change to a new value.

.. inheritance-diagram:: HABApp.core.events.ValueCommandEvent
:parts: 1

.. autoclass:: HABApp.core.events.ValueCommandEvent
:members:
:inherited-members:




ItemNoUpdateEvent
======================================

Expand Down
5 changes: 5 additions & 0 deletions docs/rule.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ ValueChangeEventFilter
.. autoclass:: HABApp.core.events.ValueChangeEventFilter
:members:

ValueCommandEventFilter
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autoclass:: HABApp.core.events.ValueCommandEventFilter
:members:

AndFilterGroup
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autoclass:: HABApp.core.events.AndFilterGroup
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ This is a breaking change!

Changelog:
- Switched to new scheduler
- Added ``ValueCommandEvent``. The openhab command event inherits from this event.

Migration of rules:
- Search for ``self.run.at`` and replace with ``self.run.once``
Expand Down
20 changes: 16 additions & 4 deletions src/HABApp/core/events/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
from . import habapp_events
from .events import ComplexEventValue, ValueUpdateEvent, ValueChangeEvent, \
ItemNoChangeEvent, ItemNoUpdateEvent
from .filter import NoEventFilter, OrFilterGroup, AndFilterGroup, ValueUpdateEventFilter, ValueChangeEventFilter, \
EventFilter
from .events import (
ComplexEventValue,
ItemNoChangeEvent,
ItemNoUpdateEvent,
ValueChangeEvent,
ValueCommandEvent,
ValueUpdateEvent,
)
from .filter import (
AndFilterGroup,
EventFilter,
NoEventFilter,
OrFilterGroup,
ValueChangeEventFilter,
ValueUpdateEventFilter,
)
27 changes: 22 additions & 5 deletions src/HABApp/core/events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class ComplexEventValue:
def __init__(self, value) -> None:
def __init__(self, value: Any) -> None:
self.value: Any = value


Expand All @@ -20,7 +20,7 @@ def __init__(self, name: str, value: Any) -> None:
self.value: Final = value

def __repr__(self) -> str:
return f'<{self.__class__.__name__} name: {self.name}, value: {self.value}>'
return f'<{self.__class__.__name__} name: {self.name:s}, value: {self.value}>'


class ValueChangeEvent:
Expand All @@ -40,7 +40,24 @@ def __init__(self, name: str, value: Any, old_value: Any) -> None:
self.old_value: Final = old_value

def __repr__(self) -> str:
return f'<{self.__class__.__name__} name: {self.name}, value: {self.value}, old_value: {self.old_value}>'
return f'<{self.__class__.__name__} name: {self.name:s}, value: {self.value}, old_value: {self.old_value}>'


class ValueCommandEvent:
"""
:ivar str name:
:ivar Any value:
"""

name: str
value: Any

def __init__(self, name: str, value: Any) -> None:
self.name: Final = name
self.value: Final = value

def __repr__(self) -> str:
return f'<{self.__class__.__name__} name: {self.name:s}, value: {self.value}>'


class ItemNoChangeEvent:
Expand All @@ -57,7 +74,7 @@ def __init__(self, name: str, seconds: int | float) -> None:
self.seconds: Final = seconds

def __repr__(self) -> str:
return f'<{self.__class__.__name__} name: {self.name}, seconds: {self.seconds}>'
return f'<{self.__class__.__name__} name: {self.name:s}, seconds: {self.seconds}>'


class ItemNoUpdateEvent:
Expand All @@ -73,4 +90,4 @@ def __init__(self, name: str, seconds: int | float) -> None:
self.seconds: Final = seconds

def __repr__(self) -> str:
return f'<{self.__class__.__name__} name: {self.name}, seconds: {self.seconds}>'
return f'<{self.__class__.__name__} name: {self.name:s}, seconds: {self.seconds}>'
6 changes: 3 additions & 3 deletions src/HABApp/core/events/filter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .no_filter import NoEventFilter
from .event import EventFilter
from .habapp_events import ValueUpdateEventFilter, ValueChangeEventFilter
from .groups import OrFilterGroup, AndFilterGroup
from .groups import AndFilterGroup, OrFilterGroup
from .habapp_events import ValueChangeEventFilter, ValueCommandEventFilter, ValueUpdateEventFilter
from .no_filter import NoEventFilter
7 changes: 6 additions & 1 deletion src/HABApp/core/events/filter/habapp_events.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any

from HABApp.core.const import MISSING
from HABApp.core.events import ValueChangeEvent, ValueUpdateEvent
from HABApp.core.events import ValueChangeEvent, ValueCommandEvent, ValueUpdateEvent
from HABApp.core.events.filter.event import TypeBoundEventFilter


Expand All @@ -13,3 +13,8 @@ def __init__(self, value: Any = MISSING) -> None:
class ValueChangeEventFilter(TypeBoundEventFilter):
def __init__(self, value: Any = MISSING, old_value: Any = MISSING) -> None:
super().__init__(ValueChangeEvent, value=value, old_value=old_value)


class ValueCommandEventFilter(TypeBoundEventFilter):
def __init__(self, value: Any = MISSING) -> None:
super().__init__(ValueCommandEvent, value=value)
14 changes: 1 addition & 13 deletions src/HABApp/openhab/events/item_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,7 @@ def __repr__(self) -> str:
return f'<{self.__class__.__name__} name: {self.name}, value: {self.value}, old_value: {self.old_value}>'


class ItemCommandEvent(OpenhabEvent):
"""
:ivar str name:
:ivar Any value:
"""
name: str
value: Any

def __init__(self, name: str, value: Any) -> None:
super().__init__()

self.name: str = name
self.value: Any = value
class ItemCommandEvent(OpenhabEvent, HABApp.core.events.ValueCommandEvent):

@classmethod
def from_dict(cls, topic: str, payload: dict):
Expand Down

0 comments on commit 534acc7

Please sign in to comment.