Skip to content

Commit

Permalink
renamed to MqttPublishOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Oct 30, 2024
1 parent 1d580fa commit d5d9a68
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 20 deletions.
11 changes: 6 additions & 5 deletions docs/interface_mqtt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ and it will also trigger for :class:`~HABApp.mqtt.events.MqttValueChangeEvent`.
Mqtt util
--------------------------------------

MqttTopicInfo
MqttPublishOptions
""""""""""""""""""""""""""""""""""""""

.. autoclass:: HABApp.mqtt.util.MqttTopicInfo
.. autoclass:: HABApp.mqtt.util.MqttPublishOptions
:members:
:inherited-members:
:member-order: groupwise
Expand All @@ -184,14 +184,15 @@ MqttTopicInfo
from unittest.mock import MagicMock
HABApp.mqtt.util.topic_info.publish = MagicMock()
# ------------ hide: stop -------------
from HABApp.mqtt.util import MqttTopicInfo
from HABApp.mqtt.util import MqttPublishOptions

topic = MqttTopicInfo('my/output/only/topic')
topic = MqttPublishOptions('my/output/only/topic')
topic.publish('new_value')

topic_qos = MqttTopicInfo('my/output/only/topic', qos=2)
topic_qos = MqttPublishOptions('my/output/only/topic', qos=2)
topic_qos.publish('new_value')

# create new through replace command wich will use qos=2 and retain=True
topic_qos_retain = topic_qos.replace(retain=True)
topic_qos_retain.publish('new_value')

Expand Down
2 changes: 1 addition & 1 deletion run/conf_testing/rules/openhab/test_item_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(self) -> None:
# oh_send_command and command_value is the same for openhab items
for name in ('oh_post_update', 'oh_send_command', 'command_value'):
for k in get_openhab_test_types():
if name == 'oh_send_command' and k == 'Contact':
if name in ('oh_send_command', 'command_value') and k == 'Contact':
continue
self.add_test(f'{k}.{name}', self.test_func, k, name, get_openhab_test_states(k))

Expand Down
4 changes: 2 additions & 2 deletions run/conf_testing/rules/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from HABApp.core.events import ValueUpdateEventFilter
from HABApp.mqtt.events import MqttValueUpdateEventFilter
from HABApp.mqtt.items import MqttItem, MqttPairItem
from HABApp.mqtt.util import MqttTopicInfo
from HABApp.mqtt.util import MqttPublishOptions


log = logging.getLogger('HABApp.MqttTestEvents')
Expand Down Expand Up @@ -91,7 +91,7 @@ async def trigger_reconnect(self) -> None:
connection.advance_status_task.start_if_not_running()

def test_mqtt_topic_info(self) -> None:
t = MqttTopicInfo('test/event_topic')
t = MqttPublishOptions('test/event_topic')
with EventWaiter(t.topic, ValueUpdateEventFilter()) as waiter:
t.publish('asdf')
waiter.wait_for_event(value='asdf')
Expand Down
2 changes: 1 addition & 1 deletion src/HABApp/mqtt/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .topic_info import MqttTopicInfo
from .publish_options import MqttPublishOptions
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from HABApp.mqtt.interface_sync import publish


class MqttTopicInfo:
class MqttPublishOptions:
"""Allows to store the topic, qos and retain settings for a topic. These values can then be used to publish
"""
def __init__(self, topic: str, qos: int | None = None, retain: bool | None = None) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/HABApp/openhab/map_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def map_item(name: str, type: str, value: str | None,
return cls.from_oh(name, value, label=label, tags=tags, groups=groups, metadata=meta)

msg = f'Unknown openHAB type: {type} for {name}'
raise ValueError(msg)
raise ValueError(msg) # noqa: TRY301

except Exception as e:
process_exception('map_items', e, logger=log)
process_exception(map_item, e, logger=log)
return None
4 changes: 3 additions & 1 deletion tests/test_openhab/test_items/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ def test_number_unit_of_measurement() -> None:

def test_datetime() -> None:

offset_str = SystemDateTime.now().format_common_iso()[-6:].replace(':', '')
# We have to build the offset str dynamically otherwise we will fail during CI because it's in another timezone
offset_str = SystemDateTime(2022, 6, 15).format_common_iso()[-6:].replace(':', '')

def get_dt(value: str):
assert value.startswith('2022-06-15') # Date must match with offset_str
return map_item(
'test1', 'DateTime', f'{value}{offset_str}', label='', tags=frozenset(), groups=frozenset(), metadata={})

Expand Down
10 changes: 3 additions & 7 deletions tests/test_openhab/test_openhab_datatypes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime

import pytest
from whenever import SystemDateTime

from HABApp.openhab.items import DatetimeItem, NumberItem
from HABApp.openhab.map_values import map_openhab_values
Expand Down Expand Up @@ -38,12 +39,7 @@ def test_type_decimal(value: str, target: int) -> None:
def __get_dt_parms():

# We have to build the offset str dynamically otherwise we will fail during CI because it's in another timezone
now = datetime.now()
offset_secs = int(now.astimezone().tzinfo.utcoffset(now).total_seconds())
hours = offset_secs // 3600
minutes = (offset_secs - 3600 * hours) // 60
assert offset_secs - hours * 3600 - minutes * 60 == 0
offset_str = f'{hours:02d}:{minutes:02d}'
offset_str = SystemDateTime(2023, 6, 17).format_common_iso()[-5:].replace(':', '')

return (
pytest.param(f'2023-06-17T15:31:04.754673068+{offset_str}', datetime(2023, 6, 17, 15, 31, 4, 754673), id='T1'),
Expand All @@ -52,7 +48,7 @@ def __get_dt_parms():
)


@pytest.mark.parametrize('value, target', __get_dt_parms())
@pytest.mark.parametrize(('value', 'target'), __get_dt_parms())
def test_type_datetime(value: str, target: datetime) -> None:
assert DatetimeItem._state_from_oh_str(value) == target
assert map_openhab_values('DateTime', value) == target
Expand Down

0 comments on commit d5d9a68

Please sign in to comment.