def test_handle_event_default(self): # {"topic":"smarthome/items/valSockMeasPower/state","payload":"{\"type\":\"Decimal\",\"value\":\"109.01\"}","type":"ItemStateEvent"} # noinspection PyPep8 str_in = '{"topic":"smarthome/items/valSockMeasPower/state","payload":"{\\\"type\\\":\\\"Decimal\\\",\\\"value\\\":\\\"109.01\\\"}","type":"ItemStateEvent"}' # noqa oh_channel = Channel.create(ChannelType.ITEM, 'valSockMeasPower') oh_state = State.create(StateType.DECIMAL, 109.01) ev_cmp = OhEvent.create(OhNotificationType.ITEM_CHANGE, oh_channel, oh_state) self.check_handle_event(str_in, ev_cmp)
def create_from_event(event: OhEvent) -> 'Action': if not event or not event.is_valid(): raise ActionException( 'invalid action ({})'.format(str(event) if event else 'None')) action = Action() action.channel = event.channel action.state_new = event.state action.notification_type = event.notification_type return action
def test_is_valid(self): event = OhEvent() event.notification_type = OhNotificationType.IGNORE self.assertTrue(not event.is_valid()) event.notification_type = OhNotificationType.RELOAD self.assertTrue(event.is_valid())
def test_fetch_thing(self): rest = OhRest(SetupTest.get_config()) time_start_loading = datetime.datetime.now() # noinspection PyPep8 json_data = { 'statusInfo': { 'status': 'ONLINE', 'statusDetail': 'NONE' }, 'editable': False, 'label': 'Homematic Bridge', 'configuration': { 'cuxdPort': 8701, 'socketMaxAlive': 900, 'installModeDuration': 60, 'reconnectInterval': 3600, 'timeout': 15, 'hmIpPort': 2010, 'discoveryTimeToLive': -1, 'wiredPort': 2000, 'gatewayType': 'ccu', 'callbackHost': '127.0.0.1', 'groupPort': 9292, 'gatewayAddress': '127.0.0.1', 'unpairOnDeletion': False, 'rfPort': 2001 }, 'properties': { 'serialNumber': 'NEQ1327832', 'firmwareVersion': '2.29.23.20171118', 'modelId': 'CCU2' }, 'UID': 'homematic:bridge:rhm', 'thingTypeUID': 'homematic:bridge', 'channels': [] } # noqa ev_out = rest._fetch_thing(time_start_loading, json_data) channel = Channel.create(ChannelType.THING, 'homematic:bridge:rhm') state = State.create(StateType.THING_STATUS, ThingStatusValue.ONLINE) ev_cmp = OhEvent.create(OhNotificationType.THING_CHANGE, channel, state) self.check_fetch_event(ev_out, ev_cmp)
def _handle_event(self, event_in) -> None: if not event_in or not event_in.data: _logger.error('invalid event (None)') return try: event_out = OhEvent.create_from_notify_json(event_in.data) if event_out.notification_type == OhNotificationType.IGNORE: pass elif event_out.is_valid(): self._gateway.push_event(event_out) # _logger.debug('event queued: %s', event_out) else: raise OhObserverException('invalid event!') except json.decoder.JSONDecodeError: _logger.error('cannot parse faulty json: %s', event_in.data) except Exception as ex: _logger.exception(ex) _logger.error('cannot parse faulty json: %s', event_in.data)
def test_repr(self): # no crash! event = OhEvent.create_empty() print(event)
def test_fetch_items(self): dispatcher = MockDispatcher() rest = MockRest() gateway = OhGateway() gateway.set_dispatcher(dispatcher) gateway.set_rest(rest) # prepare rest ev_update = OhEvent.create( OhNotificationType.ITEM_CHANGE, Channel.create(ChannelType.ITEM, 'dummySwitch'), State.create(StateType.SWITCH, OnOffValue.ON)) rest.dummy_events.append(ev_update) ev_dummy = OhEvent.create(OhNotificationType.GROUP_CHANGE, Channel.create(ChannelType.GROUP, 'gMarc'), State.create(StateType.GROUP, None)) rest.dummy_events.append(ev_dummy) ev_dummy = OhEvent.create( OhNotificationType.GROUP_CHANGE, Channel.create(ChannelType.GROUP, 'gShutter'), State.create(StateType.GROUP, '0')) rest.dummy_events.append(ev_dummy) ev_dummy = OhEvent.create( OhNotificationType.THING_CHANGE, Channel.create(ChannelType.THING, 'homematic:bridge:rhm'), State.create(StateType.THING_STATUS, ThingStatusValue.ONLINE)) rest.dummy_events.append(ev_dummy) ev_dummy = OhEvent.create( OhNotificationType.ITEM_CHANGE, Channel.create(ChannelType.ITEM, 'to_be_remove'), State.create(StateType.SWITCH, OnOffValue.ON)) gateway.push_event(ev_dummy) states_comp = gateway.get_states() self.assertEqual(1, len(states_comp)) self.assertEqual(1, len(dispatcher.queued_actions)) dispatcher.queued_actions.clear() gateway.cache_states() states_comp = gateway.get_states() self.assertEqual(len(rest.dummy_events), len(states_comp)) # find a for event_r in rest.dummy_events: state_g = states_comp.get(event_r.channel) self.assertTrue(state_g is not None) equal1 = (event_r.state == state_g) state_g2 = gateway.get_state(event_r.channel) equal2 = (event_r.state == state_g2) if not equal1 or not equal2: print( 'event_r.state == state_g:\n state_src: {}\n state_out: {}' ) self.assertEqual(state_g, event_r.state) del states_comp[event_r.channel] self.assertEqual(0, len(states_comp)) self.assertEqual(len(rest.dummy_events), len(dispatcher.queued_actions)) states_comp = dispatcher.queued_actions for event_r in rest.dummy_events: queued_action = dispatcher.queued_actions.get(event_r.channel) self.assertTrue(queued_action is not None) equal = (event_r.state == queued_action.state_new) if not equal: print( 'event_r.state == queued_action.state_new:\n state_src: {}\n state_out: {}' ) self.assertEqual(queued_action.state_new, event_r.state) del dispatcher.queued_actions[event_r.channel] self.assertEqual(0, len(states_comp)) dispatcher.queued_actions.clear() ev_update.state.value = OnOffValue.OFF ev_update.state.update_last_change() gateway.push_event(ev_update) state_g3 = gateway.get_state(ev_update.channel) self.assertEqual(ev_update.state, state_g3) self.assertEqual(1, len(dispatcher.queued_actions)) dispatcher.queued_actions.clear() gateway.push_event(ev_update) self.assertEqual(0, len(dispatcher.queued_actions)) # no change ev_update.notification_type = OhNotificationType.ITEM_COMMAND dispatcher.queued_actions.clear() gateway.push_event(ev_update) self.assertEqual(1, len(dispatcher.queued_actions))
def test_fetch_item(self): rest = OhRest(SetupTest.get_config()) time_start_loading = datetime.datetime.now() # test item # noinspection PyPep8 json_data = { 'link': 'http://127.0.0.1:8080/rest/items/dummySwitch', 'state': 'ON', 'editable': False, 'type': 'Switch', 'name': 'dummySwitch', 'category': 'settings', 'tags': [], 'groupNames': [] } # noqa ev_out = rest._fetch_item(time_start_loading, json_data) channel = Channel.create(ChannelType.ITEM, 'dummySwitch') state = State.create(StateType.SWITCH, OnOffValue.ON) ev_cmp = OhEvent.create(OhNotificationType.ITEM_CHANGE, channel, state) self.check_fetch_event(ev_out, ev_cmp) # test groups # noinspection PyPep8 json_data = { 'members': [], 'link': 'http://homeserver:8080/rest/items/gMarc', 'state': 'NULL', 'editable': False, 'type': 'Group', 'name': 'gMarc', 'label': 'Zimmer Marc', 'category': 'boy_1', 'tags': [], 'groupNames': [] } # noqa ev_out = rest._fetch_item(time_start_loading, json_data) channel = Channel.create(ChannelType.GROUP, 'gMarc') state = State.create(StateType.GROUP, None) ev_cmp = OhEvent.create(OhNotificationType.GROUP_CHANGE, channel, state) self.check_fetch_event(ev_out, ev_cmp) # noinspection PyPep8 json_data = { 'members': [], 'groupType': 'Rollershutter', 'function': { 'name': 'EQUALITY' }, 'link': 'http://homeserver:8080/rest/items/gShutter', 'state': '0', 'editable': False, 'type': 'Group', 'name': 'gShutter', 'label': 'Alle Rollläden', 'category': 'rollershutter-50', 'tags': [], 'groupNames': [] } # noqa ev_out = rest._fetch_item(time_start_loading, json_data) channel = Channel.create(ChannelType.GROUP, 'gShutter') state = State.create(StateType.GROUP, '0') ev_cmp = OhEvent.create(OhNotificationType.GROUP_CHANGE, channel, state) self.check_fetch_event(ev_out, ev_cmp)