def test_to_string(self): self.assertEqual( str(RockerAction(RockerPress.PRESS_SHORT, RockerButton.ROCK1)), "PRESS_SHORT-ROCK1") self.assertEqual( repr(RockerAction(RockerPress.PRESS_LONG, RockerButton.ROCK2)), "RockerAction(PRESS_LONG-ROCK2)")
def get_actions(cls): actions = [RockerAction(RockerPress.RELEASE, None)] for p in [RockerPress.PRESS_LONG, RockerPress.PRESS_SHORT]: for b in RockerButton: actions.append(RockerAction(button=b, press=p)) return actions
def test_process_enocean_message_short_for_long(self): # only short configued action_long = RockerAction(RockerPress.PRESS_LONG, RockerButton.ROCK1) action_short = RockerAction(RockerPress.PRESS_SHORT, action_long.button) channel_config = self.get_test_channel(action_short) device = self.create_device_for_process_enocean_message() self.simu_packet_for_process_enocean_message(device, action_long) self.check_messages_for_process_enocean_message(device, channel_config)
def test_extract_release(self): packet = PickleTools.unpickle_packet(PACKET_RELEASE) device = _MockDevice() extracted = EnoceanTools.extract_packet_props(packet, device._eep) action = RockerAction(press=RockerPress.RELEASE) expected = RockerSwitchTools.create_props(action) self.assertEqual(extracted, expected)
def test_process_enocean_message_long(self): action = RockerAction(RockerPress.PRESS_LONG, RockerButton.ROCK0) channel_config = self.get_test_channel(action) device = self.create_device_for_process_enocean_message() self.simu_packet_for_process_enocean_message(device, action) self.check_messages_for_process_enocean_message(device, channel_config)
def test_proceed_enocean(self): device = self.device Scenario = namedtuple("Scenario", ["rocker_button", "expected_state"]) scenarios = [ Scenario(RockerButton.ROCK3, "on"), Scenario(RockerButton.ROCK2, "off"), ] for scenario in scenarios: action = RockerAction(RockerPress.PRESS_SHORT, scenario.rocker_button) packet = RockerSwitchTools.create_packet(action) packet.dBm = -55 message = EnoceanMessage(payload=packet, enocean_id=device._enocean_target) device.messages = [] device.process_enocean_message(message) self.assertEqual(device._last_refresh_time, device._now()) self.assertEqual(len(device.messages), 1) result = json.loads(device.messages[0]) compare = {'timestamp': '2020-01-01T02:02:03+00:00', 'status': scenario.expected_state, 'device': 'mock'} self.assertEqual(result, compare)
def _create_switch_packet(self, switch_action, learn=False, destination=None): # simulate rocker switch if switch_action == RockerSwitchAction.ON: rocker_action = RockerAction(RockerPress.PRESS_SHORT, RockerButton.ROCK1) elif switch_action == RockerSwitchAction.OFF: rocker_action = RockerAction(RockerPress.PRESS_SHORT, RockerButton.ROCK0) elif switch_action == RockerSwitchAction.RELEASE: rocker_action = RockerAction(RockerPress.RELEASE) else: raise RuntimeError() destination = destination or self._enocean_target or 0xffffffff return RockerSwitchTools.create_packet(rocker_action, destination=destination, sender=self._enocean_sender, learn=learn)
def create_device_for_process_enocean_message(cls): device = _MockDevice() device.set_config({ CONFKEY_ENOCEAN_TARGET: 1001, # CONFKEY_ENOCEAN_SENDER: 123, CONFKEY_MQTT_CHANNEL_BTN_0: cls.get_test_channel( RockerAction(RockerPress.PRESS_SHORT, RockerButton.ROCK0)), CONFKEY_MQTT_CHANNEL_BTN_1: cls.get_test_channel( RockerAction(RockerPress.PRESS_SHORT, RockerButton.ROCK1)), CONFKEY_MQTT_CHANNEL_BTN_LONG_0: cls.get_test_channel( RockerAction(RockerPress.PRESS_LONG, RockerButton.ROCK0)), CONFKEY_MQTT_CHANNEL_BTN_LONG_2: cls.get_test_channel( RockerAction(RockerPress.PRESS_LONG, RockerButton.ROCK2)), }) return device
def test_1(self): actions = [RockerAction(RockerPress.RELEASE, None)] # for p in [RockerPress.PRESS_LONG, RockerPress.PRESS_SHORT]: # for b in RockerButton: # actions.append(RockerAction(button=b, press=p)) for action_in in actions: props_in = RockerSwitchTools.create_props(action_in) packet = RockerSwitchTools.create_packet(action_in) props_out = RockerSwitchTools.extract_props(packet) actions_out = RockerSwitchTools.extract_action(props_out) self.assertEqual(props_out, props_in) self.assertEqual(actions_out, action_in)
def test_extract_press(self): loop_data = [ (PACKET_0_PRESS, RockerButton.ROCK0), (PACKET_1_PRESS, RockerButton.ROCK1), (PACKET_2_PRESS, RockerButton.ROCK2), (PACKET_3_PRESS, RockerButton.ROCK3), ] for i in range(0, 3): packet = PickleTools.unpickle_packet(loop_data[i][0]) device = _MockDevice() extracted = EnoceanTools.extract_packet_props(packet, device._eep) action = RockerAction(press=RockerPress.PRESS_SHORT, button=loop_data[i][1]) expected = RockerSwitchTools.create_props(action) self.assertEqual(extracted, expected)
def test_process_enocean_message_release_with_empty_channel(self): # only channel_state configured action = RockerAction(RockerPress.RELEASE) device = _MockDevice() # do default channel! # device._mqtt_channel_state = "" device._mqtt_channels = { 0: CONFKEY_MQTT_CHANNEL_BTN_0, 2: CONFKEY_MQTT_CHANNEL_BTN_2, } device._mqtt_channels_long = { 0: CONFKEY_MQTT_CHANNEL_BTN_LONG_0, 2: CONFKEY_MQTT_CHANNEL_BTN_LONG_2, } self.simu_packet_for_process_enocean_message(device, action) self.assertEqual(len(device.mqtt_messages), 0)
def prepare(press, button, sender): device.clear() rocker_action = RockerAction(press=press, button=button) packet = RockerSwitchTools.create_packet(action=rocker_action, sender=sender) message = EnoceanMessage(payload=packet, enocean_id=packet.sender_int) device.process_enocean_message(message)