def test_ItemAddedEvent1(oh2_event): event = get_event({'topic': 'smarthome/items/TestString/added', 'payload': '{"type":"String","name":"TestString","tags":[],"groupNames":["TestGroup"]}', 'type': 'ItemAddedEvent'}) assert isinstance(event, ItemAddedEvent) assert event.name == 'TestString' assert event.type == 'String'
def test_ItemStatePredictedEvent(oh2_event): event = get_event({'topic': 'smarthome/items/Buero_Lampe_Vorne_W/statepredicted', 'payload': '{"predictedType":"Percent","predictedValue":"10","isConfirmation":false}', 'type': 'ItemStatePredictedEvent'}) assert isinstance(event, ItemStatePredictedEvent) assert event.name == 'Buero_Lampe_Vorne_W' assert event.value.value == 10.0
def test_ItemStateChangedEvent1(oh2_event): event = get_event({'topic': 'smarthome/items/Ping/statechanged', 'payload': '{"type":"String","value":"1","oldType":"UnDef","oldValue":"NULL"}', 'type': 'ItemStateChangedEvent'}) assert isinstance(event, ItemStateChangedEvent) assert event.name == 'Ping' assert event.value == '1' assert event.old_value is None
def test_thing_status_events(test_thing: Thing): assert test_thing.status == '' e = get_event(get_dict('ONLINE')) assert isinstance(e, ThingStatusInfoEvent) test_thing.process_event(e) assert test_thing.status == 'ONLINE' e = get_event(get_dict('asdf')) assert isinstance(e, ThingStatusInfoEvent) test_thing.process_event(e) assert test_thing.status == 'asdf' e = get_event(get_dict('NONE')) assert isinstance(e, ThingStatusInfoEvent) test_thing.process_event(e) assert test_thing.status is None
def test_ItemAddedEvent2(oh2_event): event = get_event({ 'topic': 'smarthome/items/TestColor_OFF/added', 'payload': '{"type":"Color","name":"TestColor_OFF","tags":[],"groupNames":["TestGroup"]}', 'type': 'ItemAddedEvent' }) assert isinstance(event, ItemAddedEvent) assert event.name == 'TestColor_OFF' assert event.type == 'Color'
def test_ItemCommandEvent(oh2_event): event = get_event({ 'topic': 'smarthome/items/Ping/command', 'payload': '{"type":"String","value":"1"}', 'type': 'ItemCommandEvent' }) assert isinstance(event, ItemCommandEvent) assert event.name == 'Ping' assert event.value == '1'
def test_ItemStateEvent(): event = get_event({ 'topic': 'openhab/items/Ping/state', 'payload': '{"type":"String","value":"1"}', 'type': 'ItemStateEvent' }) assert isinstance(event, ItemStateEvent) assert event.name == 'Ping' assert event.value == '1'
def test_ItemUpdatedEvent(oh2_event): event = get_event({ 'topic': 'smarthome/items/NameUpdated/updated', 'payload': '[{"type":"Switch","name":"Test","tags":[],"groupNames":[]},' '{"type":"Contact","name":"Test","tags":[],"groupNames":[]}]', 'type': 'ItemUpdatedEvent' }) assert isinstance(event, ItemUpdatedEvent) assert event.name == 'NameUpdated' assert event.type == 'Switch'
def test_thing_FirmwareStatusEvent(oh2_event): data = { 'topic': 'smarthome/things/zigbee:device:12345678:9abcdefghijklmno/firmware/status', 'payload': '{"thingUID":{"segments":["zigbee","device","12345678","9abcdefghijklmno"]},"firmwareStatus":"UNKNOWN"}', 'type': 'FirmwareStatusInfoEvent' } event = get_event(data) assert isinstance(event, ThingFirmwareStatusInfoEvent) assert event.status == 'UNKNOWN'
def test_thing_ConfigStatusInfoEvent(oh2_event): data = { 'topic': 'smarthome/things/zwave:device:controller:my_node/config/status', 'payload': '{"configStatusMessages":[{"parameterName":"switchall_mode","type":"PENDING"}]}', 'type': 'ConfigStatusInfoEvent' } event = get_event(data) assert isinstance(event, ThingConfigStatusInfoEvent) assert event.name == 'zwave:device:controller:my_node' assert event.messages == [{"parameterName": "switchall_mode", "type": "PENDING"}]
def on_sse_event(event_dict: dict): try: # Lookup corresponding OpenHAB event event = get_event(event_dict) # Update item in registry BEFORE posting to the event bus # so the items have the correct state when we process the event in a rule try: if isinstance(event, ValueUpdateEvent): __item = Items.get_item( event.name ) # type: HABApp.core.items.base_item.BaseValueItem __item.set_value(event.value) EventBus.post_event(event.name, event) return None if isinstance(event, ThingStatusInfoEvent): __thing = Items.get_item( event.name) # type: HABApp.openhab.items.Thing __thing.process_event(event) EventBus.post_event(event.name, event) return None # Workaround because there is no GroupItemStateEvent if isinstance(event, GroupItemStateChangedEvent): __item = Items.get_item( event.name) # type: HABApp.openhab.items.GroupItem __item.set_value(event.value) EventBus.post_event(event.name, event) return None except ItemNotFoundException: log_warning( log, f'Received {event.__class__.__name__} for {event.name} but item does not exist!' ) # Post the event anyway EventBus.post_event(event.name, event) return None if isinstance(event, ItemRemovedEvent): remove_from_registry(event.name) EventBus.post_event(event.name, event) return None # These events require that we query openhab because of the metadata so we have to do it in a task # They also change the item registry if isinstance(event, (ItemAddedEvent, ItemUpdatedEvent)): create_task(item_event(event)) return None HABApp.core.EventBus.post_event(event.name, event) except Exception as e: process_exception(func=on_sse_event, e=e) return None
def test_ChannelTriggeredEvent(oh2_event): d = { "topic": "smarthome/channels/mihome:sensor_switch:00000000000000:button/triggered", "payload": "{\"event\":\"SHORT_PRESSED\",\"channel\":\"mihome:sensor_switch:11111111111111:button\"}", "type": "ChannelTriggeredEvent" } event = get_event(d) assert isinstance(event, ChannelTriggeredEvent) assert event.name == 'mihome:sensor_switch:00000000000000:button' assert event.channel == 'mihome:sensor_switch:11111111111111:button' assert event.event == 'SHORT_PRESSED'
def test_GroupItemStateChangedEvent(oh2_event): d = { 'topic': 'smarthome/items/TestGroupAVG/TestNumber1/statechanged', 'payload': '{"type":"Decimal","value":"16","oldType":"Decimal","oldValue":"15"}', 'type': 'GroupItemStateChangedEvent' } event = get_event(d) assert isinstance(event, GroupItemStateChangedEvent) assert event.name == 'TestGroupAVG' assert event.item == 'TestNumber1' assert event.value == 16 assert event.old_value == 15
def on_sse_event(event_dict: dict): # Lookup corresponding OpenHAB event event = get_event(event_dict) # Update item in registry BEFORE posting to the event bus # so the items have the correct state when we process the event in a rule try: if isinstance(event, HABApp.core.events.ValueUpdateEvent): __item = Items.get_item( event.name) # type: HABApp.core.items.base_item.BaseValueItem __item.set_value(event.value) HABApp.core.EventBus.post_event(event.name, event) return None elif isinstance(event, HABApp.openhab.events.ThingStatusInfoEvent): __thing = Items.get_item( event.name) # type: HABApp.openhab.items.Thing __thing.process_event(event) HABApp.core.EventBus.post_event(event.name, event) return None except HABApp.core.Items.ItemNotFoundException: pass # Events which change the ItemRegistry if isinstance(event, (HABApp.openhab.events.ItemAddedEvent, HABApp.openhab.events.ItemUpdatedEvent)): item = map_item(event.name, event.type, 'NULL') if item is None: return None # check already existing item so we can print a warning if something changes try: existing_item = Items.get_item(item.name) if isinstance(existing_item, item.__class__): # it's the same item class so we don't replace it! item = existing_item else: log.warning( f'Item changed type from {existing_item.__class__} to {item.__class__}' ) except Items.ItemNotFoundException: pass # always overwrite with new definition Items.set_item(item) elif isinstance(event, HABApp.openhab.events.ItemRemovedEvent): Items.pop_item(event.name) # Send Event to Event Bus HABApp.core.EventBus.post_event(event.name, event) return None
def test_thing_info_events(oh2_event): data = { 'topic': 'smarthome/things/samsungtv:tv:mysamsungtv/status', 'payload': '{"status":"ONLINE","statusDetail":"MyStatusDetail"}', 'type': 'ThingStatusInfoEvent' } event = get_event(data) assert isinstance(event, ThingStatusInfoEvent) assert event.name == 'samsungtv:tv:mysamsungtv' assert event.status == 'ONLINE' assert event.detail == 'MyStatusDetail' data = { 'topic': 'smarthome/things/chromecast:chromecast:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/status', 'payload': '{"status":"ONLINE","statusDetail":"NONE"}', 'type': 'ThingStatusInfoEvent' } event = get_event(data) assert isinstance(event, ThingStatusInfoEvent) assert event.name == 'chromecast:chromecast:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' assert event.status == 'ONLINE' assert event.detail is None
def test_thing_info_changed_events(oh2_event): data = { 'topic': 'smarthome/things/samsungtv:tv:mysamsungtv/statuschanged', 'payload': '[{"status":"OFFLINE","statusDetail":"NONE"},{"status":"ONLINE","statusDetail":"NONE"}]', 'type': 'ThingStatusInfoChangedEvent' } event = get_event(data) assert isinstance(event, ThingStatusInfoChangedEvent) assert event.name == 'samsungtv:tv:mysamsungtv' assert event.status == 'OFFLINE' assert event.detail is None assert event.old_status == 'ONLINE' assert event.old_detail is None
def test_ItemStateChangedEvent2(oh2_event): UTC_OFFSET = datetime.datetime.now().astimezone(None).strftime('%z') _in = { 'topic': 'smarthome/items/TestDateTimeTOGGLE/statechanged', 'payload': f'{{"type":"DateTime","value":"2018-06-21T19:47:08.000{UTC_OFFSET}",' f'"oldType":"DateTime","oldValue":"2017-10-20T17:46:07.000{UTC_OFFSET}"}}', 'type': 'ItemStateChangedEvent'} event = get_event(_in) assert isinstance(event, ItemStateChangedEvent) assert event.name == 'TestDateTimeTOGGLE' assert datetime.datetime(2018, 6, 21, 19, 47, 8), event.value