-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e741b8
commit ff7be05
Showing
7 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/HABApp/openhab/connection/plugins/overview_broken_links.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from typing import Final | ||
|
||
from HABApp.config import CONFIG | ||
from HABApp.core.connections import BaseConnectionPlugin | ||
from HABApp.core.internals import uses_item_registry | ||
from HABApp.core.logger import log_warning | ||
from HABApp.openhab.connection.connection import OpenhabConnection | ||
from HABApp.openhab.connection.handler.func_async import async_get_things, async_get_links | ||
|
||
PING_CONFIG: Final = CONFIG.openhab.ping | ||
|
||
Items = uses_item_registry() | ||
|
||
|
||
class BrokenLinksPlugin(BaseConnectionPlugin[OpenhabConnection]): | ||
|
||
def __init__(self, name: str | None = None): | ||
super().__init__(name) | ||
self.do_run = True | ||
|
||
async def on_online(self): | ||
if not self.do_run: | ||
return None | ||
self.do_run = False | ||
|
||
log = logging.getLogger('HABApp.openhab.links') | ||
|
||
things = await async_get_things() | ||
links = await async_get_links() | ||
|
||
available_things = {t.uid for t in things} | ||
available_channels = {c.uid for t in things for c in t.channels} | ||
|
||
for link in sorted(links, key=lambda x: x.channel): | ||
if not Items.item_exists(link.item): | ||
log_warning(log, f'Item "{link.item}" does not exist! ' | ||
f'(link between item "{link.item:s}" and channel "{link.channel:s}")') | ||
continue | ||
|
||
if link.channel not in available_channels: | ||
# check if the thing exists | ||
thing_uid, channel_id = link.channel.rsplit(':', maxsplit=1) | ||
if thing_uid in available_things: | ||
log_warning(log, f'Channel "{channel_id}" on thing "{thing_uid:s}" does not exist! ' | ||
f'(link between item "{link.item:s}" and channel "{link.channel:s}")') | ||
else: | ||
log_warning(log, f'Thing "{thing_uid:s}" does not exist! ' | ||
f'(link between item "{link.item:s}" and channel "{link.channel:s}")') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from functools import partial | ||
|
||
import HABApp.openhab.connection.plugins.overview_broken_links as plugin_module | ||
from HABApp.core.internals import ItemRegistry | ||
from HABApp.core.items import Item | ||
from HABApp.openhab.definitions.rest import ThingResp, ItemChannelLinkResp | ||
from HABApp.openhab.definitions.rest.things import ThingStatusResp, ChannelResp | ||
|
||
|
||
async def _mock_things() -> list[ThingResp]: | ||
return [ | ||
ThingResp( | ||
uid='thing_type:uid', thing_type='thing_type', | ||
status=ThingStatusResp(status='ONLINE', detail='ONLINE'), | ||
editable=False, | ||
channels=[ | ||
ChannelResp(uid='thing_type:uid:channel1', id='channel1', channel_type='channel1_type', | ||
item_type='String', kind='STATE', linked_items=[]), | ||
ChannelResp(uid='thing_type:uid:channel2', id='channel2', channel_type='channel2_type', | ||
item_type='String', kind='STATE', linked_items=[]) | ||
] | ||
) | ||
] | ||
|
||
|
||
async def _mock_links() -> list[ItemChannelLinkResp]: | ||
return [ | ||
ItemChannelLinkResp(item='item1', channel='thing_type:uid:channel1', editable=True), # okay | ||
ItemChannelLinkResp(item='item2', channel='thing_type:uid:channel1', editable=True), # item does not exist | ||
ItemChannelLinkResp(item='item1', channel='thing_type:uid:channel3', editable=True), # channel does not exist | ||
ItemChannelLinkResp(item='item1', channel='other_thing:uid:channel1', editable=True), # thing does not exist | ||
] | ||
|
||
|
||
async def test_link_warning(monkeypatch, ir: ItemRegistry, test_logs): | ||
monkeypatch.setattr(plugin_module, 'async_get_things', _mock_things) | ||
monkeypatch.setattr(plugin_module, 'async_get_links', _mock_links) | ||
|
||
ir.add_item(Item('item1')) | ||
|
||
p = plugin_module.BrokenLinksPlugin() | ||
await p.on_online() | ||
|
||
add = partial(test_logs.add_expected, 'HABApp.openhab.links', logging.WARNING) | ||
|
||
add('Item "item2" does not exist! (link between item "item2" and channel "thing_type:uid:channel1")') | ||
add('Channel "channel3" on thing "thing_type:uid" does not exist! ' | ||
'(link between item "item1" and channel "thing_type:uid:channel3")') | ||
add('Thing "other_thing:uid" does not exist! (link between item "item1" and channel "other_thing:uid:channel1")') | ||
|
||
# ensure that it runs only once | ||
async def do_raise(): | ||
raise ValueError() | ||
|
||
monkeypatch.setattr(plugin_module, 'async_get_things', do_raise) | ||
monkeypatch.setattr(plugin_module, 'async_get_links', do_raise) | ||
await p.on_online() |