def test_ItemStateEvent(self): event = get_event({ 'topic': 'smarthome/items/Ping/state', 'payload': '{"type":"String","value":"1"}', 'type': 'ItemStateEvent' }) self.assertIsInstance(event, ItemStateEvent) self.assertEqual(event.name, 'Ping') self.assertEqual(event.value, '1')
def test_ItemAddedEvent2(self): event = get_event({ 'topic': 'smarthome/items/TestColor_OFF/added', 'payload': '{"type":"Color","name":"TestColor_OFF","tags":[],"groupNames":["TestGroup"]}', 'type': 'ItemAddedEvent' }) self.assertIsInstance(event, ItemAddedEvent) self.assertEqual(event.name, 'TestColor_OFF') self.assertEqual(event.type, 'Color')
def test_ItemStatePredictedEvent(self): event = get_event({ 'topic': 'smarthome/items/Buero_Lampe_Vorne_W/statepredicted', 'payload': '{"predictedType":"Percent","predictedValue":"10","isConfirmation":false}', 'type': 'ItemStatePredictedEvent' }) self.assertIsInstance(event, ItemStatePredictedEvent) self.assertEqual(event.name, 'Buero_Lampe_Vorne_W') self.assertEqual(event.value.value, 10.0)
def test_ItemAddedEvent1(self): event = get_event({ 'topic': 'smarthome/items/TestString/added', 'payload': '{"type":"String","name":"TestString","tags":[],"groupNames":["TestGroup"]}', 'type': 'ItemAddedEvent' }) self.assertIsInstance(event, ItemAddedEvent) self.assertEqual(event.name, 'TestString') self.assertEqual(event.type, 'String')
def test_ItemStateChangedEvent1(self): event = get_event({ 'topic': 'smarthome/items/Ping/statechanged', 'payload': '{"type":"String","value":"1","oldType":"UnDef","oldValue":"NULL"}', 'type': 'ItemStateChangedEvent' }) self.assertIsInstance(event, ItemStateChangedEvent) self.assertEqual(event.name, 'Ping') self.assertEqual(event.value, '1') self.assertEqual(event.old_value, None)
def test_thing_info_events(self): data = { 'topic': 'smarthome/things/samsungtv:tv:mysamsungtv/status', 'payload': '{"status":"ONLINE","statusDetail":"NONE"}', 'type': 'ThingStatusInfoEvent' } event = get_event(data) assert event.name == 'samsungtv:tv:mysamsungtv' assert event.status == 'ONLINE' assert event.detail is None data = { 'topic': 'smarthome/things/chromecast:chromecast:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/status', 'payload': '{"status":"ONLINE","statusDetail":"NONE"}', 'type': 'ThingStatusInfoEvent' } event = get_event(data) assert event.name == 'chromecast:chromecast:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' assert event.status == 'ONLINE' assert event.detail is None
def test_ItemUpdatedEvent(self): event = get_event({ 'topic': 'smarthome/items/NameUpdated/updated', 'payload': '[{"type":"Switch","name":"Test","tags":[],"groupNames":[]},' '{"type":"Contact","name":"Test","tags":[],"groupNames":[]}]', 'type': 'ItemUpdatedEvent' }) self.assertIsInstance(event, ItemUpdatedEvent) self.assertEqual(event.name, 'NameUpdated') self.assertEqual(event.type, 'Contact')
def test_thing_info_changed_events(self): data = { 'topic': 'smarthome/things/samsungtv:tv:mysamsungtv/statuschanged', 'payload': '[{"status":"OFFLINE","statusDetail":"NONE"},{"status":"ONLINE","statusDetail":"NONE"}]', 'type': 'ThingStatusInfoChangedEvent' } event = get_event(data) 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_GroupItemStateChangedEvent(self): d = { 'topic': 'smarthome/items/TestGroupAVG/TestNumber1/statechanged', 'payload': '{"type":"Decimal","value":"16","oldType":"Decimal","oldValue":"15"}', 'type': 'GroupItemStateChangedEvent' } event = get_event(d) self.assertIsInstance(event, GroupItemStateChangedEvent) self.assertEqual(event.name, 'TestGroupAVG') self.assertEqual(event.item, 'TestNumber1') self.assertEqual(event.value, 16) self.assertEqual(event.old_value, 15)
def test_ChannelTriggeredEvent(self): 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) self.assertIsInstance(event, ChannelTriggeredEvent) self.assertEqual(event.name, 'mihome:sensor_switch:00000000000000:button') self.assertEqual(event.channel, 'mihome:sensor_switch:11111111111111:button') self.assertEqual(event.event, 'SHORT_PRESSED')
def test_ItemStateChangedEvent2(self): 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) self.assertIsInstance(event, ItemStateChangedEvent) self.assertEqual(event.name, 'TestDateTimeTOGGLE') self.assertEqual(datetime.datetime(2018, 6, 21, 19, 47, 8), event.value)
def on_sse_event(self, event_dict: dict): try: # Lookup corresponding OpenHAB event event = get_event(event_dict) # Events which change the ItemRegistry if isinstance(event, (HABApp.openhab.events.ItemAddedEvent, HABApp.openhab.events.ItemUpdatedEvent)): item = HABApp.openhab.map_items(event.name, event.type, 'NULL') # check already existing item so we can print a warning if something changes try: existing_item = HABApp.core.Items.get_item(item.name) if not isinstance(existing_item, item.__class__): log.warning( f'Item changed type from {existing_item.__class__} to {item.__class__}' ) except HABApp.core.Items.ItemNotFoundException: pass # always overwrite with new definition HABApp.core.Items.set_item(item) elif isinstance(event, HABApp.openhab.events.ItemRemovedEvent): HABApp.core.Items.pop_item(event.name) # Update Item in registry BEFORE posting to the event bus if isinstance(event, HABApp.core.events.ValueUpdateEvent): try: HABApp.core.Items.get_item(event.name).set_value( event.value) except HABApp.core.Items.ItemNotFoundException: pass # Send Event to Event Bus HABApp.core.EventBus.post_event(event.name, event) except Exception as e: log.error(e) for line in traceback.format_exc().splitlines(): log.error(line)