Skip to content

Commit

Permalink
0.15.2 (#167)
Browse files Browse the repository at this point in the history
- Both values for ValueChangeEvent are properly unpacked
- Bumped library versions
- Fixed a bug where the configuration for a link was not set properly
- The test client does log all rest communication in the testcase
  • Loading branch information
spacemanspiff2007 authored Oct 1, 2020
1 parent 020578c commit 20f6bbe
Show file tree
Hide file tree
Showing 24 changed files with 237 additions and 75 deletions.
2 changes: 1 addition & 1 deletion HABApp/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.15.1'
__version__ = '0.15.2'
1 change: 0 additions & 1 deletion HABApp/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def on_all_values_set(self):
if not self.config.is_dir():
log.info(f'Manual thing configuration disabled! Folder {self.config} does not exist!')


# add path for libraries
if self.lib.is_dir():
lib_path = str(self.lib)
Expand Down
4 changes: 3 additions & 1 deletion HABApp/core/EventBus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from HABApp.core.wrapper import log_exception
from . import EventBusListener
from .events import ComplexEventValue
from .events import ComplexEventValue, ValueChangeEvent

_event_log = logging.getLogger('HABApp.EventBus')
_habapp_log = logging.getLogger('HABApp')
Expand Down Expand Up @@ -34,6 +34,8 @@ def post_event(topic: str, event):
try:
if isinstance(event.value, ComplexEventValue):
event.value = event.value.value
if isinstance(event, ValueChangeEvent) and isinstance(event.old_value, ComplexEventValue):
event.old_value = event.old_value.value
except AttributeError:
pass

Expand Down
3 changes: 0 additions & 3 deletions HABApp/core/const/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@
from . import topics
from .const import MISSING
from .loop import loop

# utilities last!
from . import utilities
1 change: 0 additions & 1 deletion HABApp/core/const/utilities/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion HABApp/core/items/base_item_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import HABApp
from ..const import loop
from ..const.utilities import PendingFuture
from HABApp.core.lib import PendingFuture
from ..events import ItemNoChangeEvent, ItemNoUpdateEvent


Expand Down
1 change: 1 addition & 0 deletions HABApp/core/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .funcs import list_files
from .pending_future import PendingFuture
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import typing
from asyncio import Task, sleep, ensure_future
from typing import Optional, Awaitable, Callable, Any
from asyncio import Task, ensure_future, sleep
from typing import Any, Awaitable, Callable, Optional


class PendingFuture:
Expand Down
16 changes: 9 additions & 7 deletions HABApp/core/logger.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import logging

import HABApp
from .const import topics
from .const.topics import ERRORS as _T_ERRORS
from .const.topics import WARNINGS as _T_WARNINGS
from .const.topics import INFOS as _T_INFOS


def log_error(logger: logging.Logger, text: str):
logger.error(text)
HABApp.core.EventBus.post_event(
topics.ERRORS, text
_T_ERRORS, text
)


def log_warning(logger: logging.Logger, text: str):
logger.warning(text)
HABApp.core.EventBus.post_event(
topics.WARNINGS, text
_T_WARNINGS, text
)


def log_info(logger: logging.Logger, text: str):
logger.info(text)
HABApp.core.EventBus.post_event(
topics.INFOS, text
_T_INFOS, text
)


Expand Down Expand Up @@ -65,14 +67,14 @@ def __bool__(self):

class HABAppError(HABAppLogger):
_LEVEL = logging.ERROR
_TOPIC = topics.ERRORS
_TOPIC = _T_ERRORS


class HABAppWarning(HABAppLogger):
_LEVEL = logging.WARNING
_TOPIC = topics.WARNINGS
_TOPIC = _T_WARNINGS


class HABAppInfo(HABAppLogger):
_LEVEL = logging.INFO
_TOPIC = topics.INFOS
_TOPIC = _T_INFOS
3 changes: 1 addition & 2 deletions HABApp/openhab/connection_handler/func_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ async def async_get_channel_links() -> List[Dict[str, str]]:
return await ret.json(encoding='utf-8')



async def async_get_channel_link(channel_uid: str, item_name: str) -> ItemChannelLinkDefinition:
ret = await get(__get_link_url(channel_uid, item_name), log_404=False)
if ret.status == 404:
Expand All @@ -242,7 +241,7 @@ async def async_create_channel_link(channel_uid: str, item_name: str, configurat
if not await async_item_exists(item_name):
raise ItemNotFoundError.from_name(item_name)

ret = await put(__get_link_url(channel_uid, item_name), json=configuration)
ret = await put(__get_link_url(channel_uid, item_name), json={'configuration': configuration})
if ret is None:
return False
return ret.status == 200
8 changes: 5 additions & 3 deletions HABApp/openhab/connection_handler/func_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,11 @@ def get_channel_link(channel_uid: str, item_name: str) -> ItemChannelLinkDefinit


def create_channel_link(channel_uid: str, item_name: str, configuration: dict = {}) -> bool:
""" creates a link between a (things) channel and an item
"""creates a link between a (things) channel and an item
:param link_def: an instance of ItemChannelLinkDefinition with at least channel_uid and item_name set
:param channel_uid: uid of the (thing) channel (usually something like AAAA:BBBBB:CCCCC:DDDD:0#SOME_NAME)
:param item_name: name of the item
:param configuration: optional configuration for the channel
:return: true on successful creation, otherwise false
"""

Expand All @@ -266,7 +268,7 @@ def create_channel_link(channel_uid: str, item_name: str, configuration: dict =
def remove_channel_link(channel_uid: str, item_name: str) -> bool:
""" removes a link between a (things) channel and an item
:param channel_uid: uid of the (things) channel (usually something like AAAA:BBBBB:CCCCC:DDDD:0#SOME_NAME)
:param channel_uid: uid of the (thing) channel (usually something like AAAA:BBBBB:CCCCC:DDDD:0#SOME_NAME)
:param item_name: name of the item
:return: true on successful removal, otherwise false
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict, Set

import HABApp
from HABApp.core.const.utilities import PendingFuture
from HABApp.core.lib import PendingFuture
from HABApp.core.logger import log_warning, HABAppError
from HABApp.openhab.connection_handler.func_async import async_get_things
from HABApp.openhab.connection_logic.plugin_things.cfg_validator import validate_cfg, InvalidItemNameError
Expand Down
4 changes: 2 additions & 2 deletions HABApp/openhab/items/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .color_item import ColorItem
from .number_item import NumberItem
from .datetime_item import DatetimeItem
from .group_item import GroupItem
from .string_item import StringItem, LocationItem, PlayerItem
from .image_item import ImageItem
from .group_item import GroupItem
from .thing_item import Thing
from .image_item import ImageItem
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include LICENSE
4 changes: 2 additions & 2 deletions _doc/util.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ Basic Example

# This shows how to enable/disable a mode and how to get a mode from the item
print('disable/enable the higher priority mode')
item.get_mode('manual').set_enabled(False)
item.get_mode('manual').set_value(11)
item.get_mode('manual').set_enabled(False) # disable mode
item.get_mode('manual').set_value(11) # setting a value will enable it again

# This shows that changes of the lower priority is only show when
# the mode with the higher priority gets disabled
Expand Down
77 changes: 77 additions & 0 deletions conf_testing/lib/HABAppTests/_rest_patcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import logging
from pprint import pformat

import HABApp.openhab.connection_handler.http_connection
from HABApp.openhab.connection_handler.http_connection import HTTP_PREFIX

FUNC_PATH = HABApp.openhab.connection_handler.func_async


def shorten_url(url: str):
url = str(url)
if url.startswith(HTTP_PREFIX):
return url[len(HTTP_PREFIX):]
return url


class RestPatcher:
def __init__(self, name):
self.log = logging.getLogger('HABApp.Rest')
self.log.debug('')
self.log.debug(f'{name}:')

def wrap(self, to_call):
async def resp_wrap(*args, **kwargs):

resp = await to_call(*args, **kwargs)

out = ''
if kwargs.get('json'):
out = f' {kwargs["json"]}'
if kwargs.get('data'):
out = f' "{kwargs["data"]}"'
self.log.debug(f'{resp.request_info.method:^6s} {shorten_url(resp.request_info.url)} ({resp.status}){out}')

def wrap_content(content_func):
async def content_func_wrap(*cargs, **ckwargs):
t = await content_func(*cargs, **ckwargs)

# pretty print the response
obj = pformat(t, indent=2)
if obj[0] == '[' and obj[-1] == ']':
obj = f'[\n {obj[1:-1]}\n]'
elif obj[0] == '{' and obj[-1] == '}':
obj = f'{{\n {obj[1:-1]}\n}}'
lines = obj.splitlines()
if len(lines) <= 1:
self.log.debug(f'{"->":6s}')
else:
for i, l in enumerate(lines):
self.log.debug(f'{"->" if not i else "":^6s} {l}')

return t
return content_func_wrap

resp.text = wrap_content(resp.text)
resp.json = wrap_content(resp.json)
return resp
return resp_wrap

def __enter__(self):
self._get = FUNC_PATH.get
self._put = FUNC_PATH.put
self._post = FUNC_PATH.post
self._delete = FUNC_PATH.delete

FUNC_PATH.get = self.wrap(self._get)
FUNC_PATH.put = self.wrap(self._put)
FUNC_PATH.post = self.wrap(self._post)
FUNC_PATH.delete = self.wrap(self._delete)

def __exit__(self, exc_type, exc_val, exc_tb):
FUNC_PATH.get = self._get
FUNC_PATH.put = self._put
FUNC_PATH.post = self._post
FUNC_PATH.delete = self._delete

return False
Loading

0 comments on commit 20f6bbe

Please sign in to comment.