Ejemplo n.º 1
0
    def test_discover_sensor_noautoadd(self):
        """Test with discover of sensor when auto add is False."""
        self.assertTrue(_setup_component(self.hass, 'sensor', {
            'sensor': {'platform': 'rfxtrx',
                       'automatic_add': False,
                       'devices': {}}}))

        event = rfxtrx_core.get_rfx_object('0a520801070100b81b0279')
        event.data = bytearray(b'\nR\x08\x01\x07\x01\x00\xb8\x1b\x02y')

        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))

        event = rfxtrx_core.get_rfx_object('0a52080405020095240279')
        event.data = bytearray(b'\nR\x08\x04\x05\x02\x00\x95$\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))

        event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279')
        event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))
Ejemplo n.º 2
0
    def test_discover_switch(self):
        """Test with discovery of switches."""
        self.assertTrue(
            _setup_component(
                self.hass, 'switch', {
                    'switch': {
                        'platform': 'rfxtrx',
                        'automatic_add': True,
                        'devices': {}
                    }
                }))

        event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
        event.data = bytearray([
            0x0b, 0x11, 0x00, 0x10, 0x01, 0x18, 0xcd, 0xea, 0x01, 0x01, 0x0f,
            0x70
        ])

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        entity = rfxtrx_core.RFX_DEVICES['118cdea2']
        self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES))
        self.assertEqual('<Entity 0b1100100118cdea01010f70: on>',
                         entity.__str__())

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES))

        event = rfxtrx_core.get_rfx_object('0b1100100118cdeb02010f70')
        event.data = bytearray([
            0x0b, 0x11, 0x00, 0x12, 0x01, 0x18, 0xcd, 0xea, 0x02, 0x00, 0x00,
            0x70
        ])

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        entity = rfxtrx_core.RFX_DEVICES['118cdeb2']
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
        self.assertEqual('<Entity 0b1100120118cdea02000070: on>',
                         entity.__str__())

        # Trying to add a sensor
        event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279')
        event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a light
        event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
        event.data = bytearray([
            0x0b, 0x11, 0x11, 0x10, 0x01, 0x18, 0xcd, 0xea, 0x01, 0x02, 0x0f,
            0x70
        ])
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a rollershutter
        event = rfxtrx_core.get_rfx_object('0a1400adf394ab020e0060')
        event.data = bytearray(
            [0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60])
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
Ejemplo n.º 3
0
    def test_discover_sensor(self):
        """Test with discovery of sensor."""
        self.assertTrue(_setup_component(self.hass, 'sensor', {
            'sensor': {'platform': 'rfxtrx',
                       'automatic_add': True,
                       'devices': {}}}))

        event = rfxtrx_core.get_rfx_object('0a520801070100b81b0279')
        event.data = bytearray(b'\nR\x08\x01\x07\x01\x00\xb8\x1b\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)

        entity = rfxtrx_core.RFX_DEVICES['sensor_0701']['Temperature']
        self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES))
        self.assertEqual({'Humidity status': 'normal',
                          'Temperature': 18.4,
                          'Rssi numeric': 7, 'Humidity': 27,
                          'Battery numeric': 9,
                          'Humidity status numeric': 2},
                         entity.device_state_attributes)
        self.assertEqual('0a520801070100b81b0279',
                         entity.__str__())

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES))

        event = rfxtrx_core.get_rfx_object('0a52080405020095240279')
        event.data = bytearray(b'\nR\x08\x04\x05\x02\x00\x95$\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        entity = rfxtrx_core.RFX_DEVICES['sensor_0502']['Temperature']
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
        self.assertEqual({'Humidity status': 'normal',
                          'Temperature': 14.9,
                          'Rssi numeric': 7, 'Humidity': 36,
                          'Battery numeric': 9,
                          'Humidity status numeric': 2},
                         entity.device_state_attributes)
        self.assertEqual('0a52080405020095240279',
                         entity.__str__())

        event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279')
        event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        entity = rfxtrx_core.RFX_DEVICES['sensor_0701']['Temperature']
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
        self.assertEqual({'Humidity status': 'normal',
                          'Temperature': 17.9,
                          'Rssi numeric': 7, 'Humidity': 27,
                          'Battery numeric': 9,
                          'Humidity status numeric': 2},
                         entity.device_state_attributes)
        self.assertEqual('0a520801070100b81b0279',
                         entity.__str__())

        # trying to add a switch
        event = rfxtrx_core.get_rfx_object('0b1100cd0213c7f210010f70')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
Ejemplo n.º 4
0
    def test_discover_switch(self):
        """Test with discovery of switches."""
        self.assertTrue(_setup_component(self.hass, 'switch', {
            'switch': {'platform': 'rfxtrx',
                       'automatic_add': True,
                       'devices': {}}}))

        event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
        event.data = bytearray([0x0b, 0x11, 0x00, 0x10, 0x01, 0x18,
                                0xcd, 0xea, 0x01, 0x01, 0x0f, 0x70])

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        entity = rfxtrx_core.RFX_DEVICES['118cdea2']
        self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES))
        self.assertEqual('<Entity 0b1100100118cdea01010f70: on>',
                         entity.__str__())

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES))

        event = rfxtrx_core.get_rfx_object('0b1100100118cdeb02010f70')
        event.data = bytearray([0x0b, 0x11, 0x00, 0x12, 0x01, 0x18,
                                0xcd, 0xea, 0x02, 0x00, 0x00, 0x70])

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        entity = rfxtrx_core.RFX_DEVICES['118cdeb2']
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
        self.assertEqual('<Entity 0b1100120118cdea02000070: on>',
                         entity.__str__())

        # Trying to add a sensor
        event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279')
        event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a light
        event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
        event.data = bytearray([0x0b, 0x11, 0x11, 0x10, 0x01, 0x18,
                                0xcd, 0xea, 0x01, 0x02, 0x0f, 0x70])
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a rollershutter
        event = rfxtrx_core.get_rfx_object('0a1400adf394ab020e0060')
        event.data = bytearray([0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94,
                                0xAB, 0x02, 0x0E, 0x00, 0x60])
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
Ejemplo n.º 5
0
    def test_discover_rollershutter(self):
        """Test with discovery of roller shutters."""
        self.assertTrue(
            _setup_component(
                self.hass, 'rollershutter', {
                    'rollershutter': {
                        'platform': 'rfxtrx',
                        'automatic_add': True,
                        'devices': {}
                    }
                }))

        event = rfxtrx_core.get_rfx_object('0a140002f38cae010f0070')
        event.data = bytearray(
            [0x0A, 0x14, 0x00, 0x02, 0xF3, 0x8C, 0xAE, 0x01, 0x0F, 0x00, 0x70])

        for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
            evt_sub(event)
        self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES))

        event = rfxtrx_core.get_rfx_object('0a1400adf394ab020e0060')
        event.data = bytearray(
            [0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60])

        for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
            evt_sub(event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a sensor
        event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279')
        event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y')
        for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
            evt_sub(event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a light
        event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
        event.data = bytearray([
            0x0b, 0x11, 0x11, 0x10, 0x01, 0x18, 0xcd, 0xea, 0x01, 0x02, 0x0f,
            0x70
        ])
        for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
            evt_sub(event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
Ejemplo n.º 6
0
    def test_fire_event(self):
        """Test fire event."""

        self.assertTrue(
            _setup_component(
                self.hass, 'rfxtrx', {
                    'rfxtrx': {
                        'device': '/dev/serial/by-id/usb' +
                        '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0',
                        'dummy': True
                    }
                }))
        self.assertTrue(
            _setup_component(
                self.hass, 'switch', {
                    'switch': {
                        'platform': 'rfxtrx',
                        'automatic_add': True,
                        'devices': {
                            '0b1100cd0213c7f210010f51': {
                                'name': 'Test',
                                rfxtrx.ATTR_FIREEVENT: True
                            }
                        }
                    }
                }))

        calls = []

        def record_event(event):
            """Add recorded event to set."""
            calls.append(event)

        self.hass.bus.listen(rfxtrx.EVENT_BUTTON_PRESSED, record_event)

        entity = rfxtrx.RFX_DEVICES['213c7f216']
        self.assertEqual('Test', entity.name)
        self.assertEqual('off', entity.state)
        self.assertTrue(entity.should_fire_event)

        event = rfxtrx.get_rfx_object('0b1100cd0213c7f210010f51')
        event.data = bytearray([
            0x0b, 0x11, 0x00, 0x10, 0x01, 0x18, 0xcd, 0xea, 0x01, 0x01, 0x0f,
            0x70
        ])
        rfxtrx.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.hass.pool.block_till_done()

        self.assertEqual(event.values['Command'], "On")
        self.assertEqual('on', entity.state)
        self.assertEqual(1, len(rfxtrx.RFX_DEVICES))
        self.assertEqual(1, len(calls))
        self.assertEqual(calls[0].data, {
            'entity_id': 'switch.test',
            'state': 'on'
        })
Ejemplo n.º 7
0
    def test_discover_rollershutter(self):
        """Test with discovery of roller shutters."""
        self.assertTrue(_setup_component(self.hass, 'rollershutter', {
            'rollershutter': {'platform': 'rfxtrx',
                              'automatic_add': True,
                              'devices': {}}}))

        event = rfxtrx_core.get_rfx_object('0a140002f38cae010f0070')
        event.data = bytearray([0x0A, 0x14, 0x00, 0x02, 0xF3, 0x8C,
                                0xAE, 0x01, 0x0F, 0x00, 0x70])

        for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
            evt_sub(event)
        self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES))

        event = rfxtrx_core.get_rfx_object('0a1400adf394ab020e0060')
        event.data = bytearray([0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94,
                                0xAB, 0x02, 0x0E, 0x00, 0x60])

        for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
            evt_sub(event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a sensor
        event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279')
        event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y')
        for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
            evt_sub(event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a light
        event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
        event.data = bytearray([0x0b, 0x11, 0x11, 0x10, 0x01, 0x18,
                                0xcd, 0xea, 0x01, 0x02, 0x0f, 0x70])
        for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
            evt_sub(event)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
Ejemplo n.º 8
0
    def test_discover_light_noautoadd(self):
        """Test with discover of light when auto add is False."""
        self.assertTrue(_setup_component(self.hass, 'light', {
            'light': {'platform': 'rfxtrx',
                      'automatic_add': False,
                      'devices': {}}}))

        event = rfxtrx_core.get_rfx_object('0b1100120118cdea02020070')
        event.data = bytearray([0x0b, 0x11, 0x00, 0x12, 0x01, 0x18,
                                0xcd, 0xea, 0x02, 0x02, 0x00, 0x70])

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))

        event = rfxtrx_core.get_rfx_object('0b1100120118cdea02010070')
        event.data = bytearray([0x0b, 0x11, 0x00, 0x12, 0x01, 0x18,
                                0xcd, 0xea, 0x02, 0x01, 0x00, 0x70])

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))

        event = rfxtrx_core.get_rfx_object('0b1100120118cdea02020070')
        event.data = bytearray([0x0b, 0x11, 0x00, 0x12, 0x01, 0x18,
                                0xcd, 0xea, 0x02, 0x02, 0x00, 0x70])

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a sensor
        event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279')
        event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a switch
        event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
        event.data = bytearray([0x0b, 0x11, 0x00, 0x10, 0x01, 0x18,
                                0xcd, 0xea, 0x01, 0x01, 0x0f, 0x70])
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))

        # Trying to add a rollershutter
        event = rfxtrx_core.get_rfx_object('0a1400adf394ab020e0060')
        event.data = bytearray([0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94,
                                0xAB, 0x02, 0x0E, 0x00, 0x60])
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        self.assertEqual(0, len(rfxtrx_core.RFX_DEVICES))
Ejemplo n.º 9
0
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """Setup the RFXtrx platform."""
    # pylint: disable=too-many-locals
    from RFXtrx import SensorEvent
    sensors = []
    for packet_id, entity_info in config['devices'].items():
        event = rfxtrx.get_rfx_object(packet_id)
        device_id = "sensor_" + slugify(event.device.id_string.lower())
        if device_id in rfxtrx.RFX_DEVICES:
            continue
        _LOGGER.info("Add %s rfxtrx.sensor", entity_info[ATTR_NAME])

        sub_sensors = {}
        data_types = entity_info[ATTR_DATA_TYPE]
        if len(data_types) == 0:
            for data_type in DATA_TYPES:
                if data_type in event.values:
                    data_types = [data_type]
                    break
        for _data_type in data_types:
            new_sensor = RfxtrxSensor(event, entity_info[ATTR_NAME],
                                      _data_type)
            sensors.append(new_sensor)
            sub_sensors[_data_type] = new_sensor
        rfxtrx.RFX_DEVICES[device_id] = sub_sensors

    add_devices_callback(sensors)

    def sensor_update(event):
        """Callback for sensor updates from the RFXtrx gateway."""
        if not isinstance(event, SensorEvent):
            return

        device_id = "sensor_" + slugify(event.device.id_string.lower())

        if device_id in rfxtrx.RFX_DEVICES:
            sensors = rfxtrx.RFX_DEVICES[device_id]
            for key in sensors:
                sensors[key].event = event
            return

        # Add entity if not exist and the automatic_add is True
        if not config[ATTR_AUTOMATIC_ADD]:
            return

        pkt_id = "".join("{0:02x}".format(x) for x in event.data)
        _LOGGER.info("Automatic add rfxtrx.sensor: %s", device_id)

        data_type = "Unknown"
        for _data_type in DATA_TYPES:
            if _data_type in event.values:
                data_type = _data_type
                break
        new_sensor = RfxtrxSensor(event, pkt_id, data_type)
        sub_sensors = {}
        sub_sensors[new_sensor.data_type] = new_sensor
        rfxtrx.RFX_DEVICES[device_id] = sub_sensors
        add_devices_callback([new_sensor])

    if sensor_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
        rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(sensor_update)
Ejemplo n.º 10
0
    def test_update_of_sensors(self):
        """Test with 3 sensors."""
        self.assertTrue(_setup_component(self.hass, 'sensor', {
                'sensor': {'platform': 'rfxtrx',
                           'devices':
                               {'0a52080705020095220269': {
                                   'name': 'Test',
                                   'data_type': 'Temperature'},
                                   '0a520802060100ff0e0269': {
                                   'name': 'Bath',
                                   'data_type': ['Temperature', 'Humidity']
                                   }}}}))

        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
        device_num = 0
        for id in rfxtrx_core.RFX_DEVICES:
            if id == 'sensor_0601':
                device_num = device_num + 1
                self.assertEqual(len(rfxtrx_core.RFX_DEVICES[id]), 2)
                _entity_temp = rfxtrx_core.RFX_DEVICES[id]['Temperature']
                _entity_hum = rfxtrx_core.RFX_DEVICES[id]['Humidity']
                self.assertEqual('%', _entity_hum.unit_of_measurement)
                self.assertEqual(14, _entity_hum.state)
                self.assertEqual({'Battery numeric': 9, 'Temperature': 25.5,
                                  'Humidity': 14, 'Humidity status': 'normal',
                                  'Humidity status numeric': 2,
                                  'Rssi numeric': 6},
                                 _entity_hum.device_state_attributes)
                self.assertEqual('Bath', _entity_hum.__str__())

                self.assertEqual(TEMP_CELSIUS,
                                 _entity_temp.unit_of_measurement)
                self.assertEqual(25.5, _entity_temp.state)
                self.assertEqual({'Battery numeric': 9, 'Temperature': 25.5,
                                  'Humidity': 14, 'Humidity status': 'normal',
                                  'Humidity status numeric': 2,
                                  'Rssi numeric': 6},
                                 _entity_temp.device_state_attributes)
                self.assertEqual('Bath', _entity_temp.__str__())
            elif id == 'sensor_0502':
                device_num = device_num + 1
                entity = rfxtrx_core.RFX_DEVICES[id]['Temperature']

                self.assertEqual(TEMP_CELSIUS, entity.unit_of_measurement)
                self.assertEqual(14.9, entity.state)
                self.assertEqual({'Humidity status': 'normal',
                                  'Temperature': 14.9,
                                  'Rssi numeric': 6, 'Humidity': 34,
                                  'Battery numeric': 9,
                                  'Humidity status numeric': 2},
                                 entity.device_state_attributes)
                self.assertEqual('Test', entity.__str__())

        self.assertEqual(2, device_num)

        event = rfxtrx_core.get_rfx_object('0a520802060101ff0f0269')
        event.data = bytearray(b'\nR\x08\x01\x07\x01\x00\xb8\x1b\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)

        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
        event = rfxtrx_core.get_rfx_object('0a52080705020085220269')
        event.data = bytearray(b'\nR\x08\x04\x05\x02\x00\x95$\x02y')
        rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)

        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))

        device_num = 0
        for id in rfxtrx_core.RFX_DEVICES:
            if id == 'sensor_0601':
                device_num = device_num + 1
                self.assertEqual(len(rfxtrx_core.RFX_DEVICES[id]), 2)
                _entity_temp = rfxtrx_core.RFX_DEVICES[id]['Temperature']
                _entity_hum = rfxtrx_core.RFX_DEVICES[id]['Humidity']
                self.assertEqual('%', _entity_hum.unit_of_measurement)
                self.assertEqual(15, _entity_hum.state)
                self.assertEqual({'Battery numeric': 9, 'Temperature': 51.1,
                                  'Humidity': 15, 'Humidity status': 'normal',
                                  'Humidity status numeric': 2,
                                  'Rssi numeric': 6},
                                 _entity_hum.device_state_attributes)
                self.assertEqual('Bath', _entity_hum.__str__())

                self.assertEqual(TEMP_CELSIUS,
                                 _entity_temp.unit_of_measurement)
                self.assertEqual(51.1, _entity_temp.state)
                self.assertEqual({'Battery numeric': 9, 'Temperature': 51.1,
                                  'Humidity': 15, 'Humidity status': 'normal',
                                  'Humidity status numeric': 2,
                                  'Rssi numeric': 6},
                                 _entity_temp.device_state_attributes)
                self.assertEqual('Bath', _entity_temp.__str__())
            elif id == 'sensor_0502':
                device_num = device_num + 1
                entity = rfxtrx_core.RFX_DEVICES[id]['Temperature']
                self.assertEqual(TEMP_CELSIUS, entity.unit_of_measurement)
                self.assertEqual(13.3, entity.state)
                self.assertEqual({'Humidity status': 'normal',
                                  'Temperature': 13.3,
                                  'Rssi numeric': 6, 'Humidity': 34,
                                  'Battery numeric': 9,
                                  'Humidity status numeric': 2},
                                 entity.device_state_attributes)
                self.assertEqual('Test', entity.__str__())

        self.assertEqual(2, device_num)
        self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
Ejemplo n.º 11
0
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """Setup the RFXtrx platform."""
    # pylint: disable=too-many-locals
    from RFXtrx import SensorEvent
    sensors = []
    for packet_id, entity_info in config['devices'].items():
        event = rfxtrx.get_rfx_object(packet_id)
        device_id = "sensor_" + slugify(event.device.id_string.lower())
        if device_id in rfxtrx.RFX_DEVICES:
            continue
        _LOGGER.info("Add %s rfxtrx.sensor", entity_info[ATTR_NAME])

        sub_sensors = {}
        data_types = entity_info[ATTR_DATA_TYPE]
        if len(data_types) == 0:
            for data_type in DATA_TYPES:
                if data_type in event.values:
                    data_types = [data_type]
                    break
        for _data_type in data_types:
            new_sensor = RfxtrxSensor(event, entity_info[ATTR_NAME],
                                      _data_type)
            sensors.append(new_sensor)
            sub_sensors[_data_type] = new_sensor
        rfxtrx.RFX_DEVICES[device_id] = sub_sensors

    add_devices_callback(sensors)

    def sensor_update(event):
        """Callback for sensor updates from the RFXtrx gateway."""
        if not isinstance(event, SensorEvent):
            return

        device_id = "sensor_" + slugify(event.device.id_string.lower())

        if device_id in rfxtrx.RFX_DEVICES:
            sensors = rfxtrx.RFX_DEVICES[device_id]
            for key in sensors:
                sensors[key].event = event
            return

        # Add entity if not exist and the automatic_add is True
        if not config[ATTR_AUTOMATIC_ADD]:
            return

        pkt_id = "".join("{0:02x}".format(x) for x in event.data)
        _LOGGER.info("Automatic add rfxtrx.sensor: %s",
                     device_id)

        data_type = "Unknown"
        for _data_type in DATA_TYPES:
            if _data_type in event.values:
                data_type = _data_type
                break
        new_sensor = RfxtrxSensor(event, pkt_id, data_type)
        sub_sensors = {}
        sub_sensors[new_sensor.data_type] = new_sensor
        rfxtrx.RFX_DEVICES[device_id] = sub_sensors
        add_devices_callback([new_sensor])

    if sensor_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
        rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(sensor_update)