# Buggy code
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """Setup the RFXtrx platform."""
    import RFXtrx as rfxtrxmod

    lights = []
    devices = config.get("devices", None)

    if devices:
        for entity_id, entity_info in devices.items():
            if entity_id not in rfxtrx.RFX_DEVICES:
                _LOGGER.info("Add %s rfxtrx.light", entity_info[ATTR_NAME])

                # Check if i must fire event
                fire_event = entity_info.get(ATTR_FIREEVENT, False)
                datas = {ATTR_STATE: False, ATTR_FIREEVENT: fire_event}

                rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
                new_light = RfxtrxLight(entity_info[ATTR_NAME], rfxobject, datas)
                rfxtrx.RFX_DEVICES[entity_id] = new_light
                lights.append(new_light)

    add_devices_callback(lights)

    def light_update(event):
        """Callback for light updates from the RFXtrx gateway."""
        if not isinstance(event.device, rfxtrxmod.LightingDevice):
            return

        # Add entity if not exist and the automatic_add is True
        entity_id = slugify(event.device.id_string.lower())
        if entity_id not in rfxtrx.RFX_DEVICES:
            automatic_add = config.get("automatic_add", False)
            if not automatic_add:
                return

            _LOGGER.info(
                "Automatic add %s rfxtrx.light (Class: %s Sub: %s)",
                entity_id,
                event.device.__class__.__name__,
                event.device.subtype,
            )
            pkt_id = "".join("{0:02x}".format(x) for x in event.data)
            entity_name = "%s : %s" % (entity_id, pkt_id)
            datas = {ATTR_STATE: False, ATTR_FIREEVENT: False}
            new_light = RfxtrxLight(entity_name, event, datas)
            rfxtrx.RFX_DEVICES[entity_id] = new_light
            add_devices_callback([new_light])

        # Check if entity exists or previously added automatically
        if entity_id in rfxtrx.RFX_DEVICES and isinstance(
            rfxtrx.RFX_DEVICES[entity_id], RfxtrxLight
        ):
            _LOGGER.debug(
                "EntityID: %s light_update. Command: %s",
                entity_id,
                event.values["Command"],
            )
            if event.values["Command"] == "On" or event.values["Command"] == "Off":
                # Update the rfxtrx device state
                is_on = event.values["Command"] == "On"
                # pylint: disable=protected-access
                rfxtrx.RFX_DEVICES[entity_id]._state = is_on
                rfxtrx.RFX_DEVICES[entity_id].update_ha_state()

                # Fire event
                if rfxtrx.RFX_DEVICES[entity_id].should_fire_event:
                    rfxtrx.RFX_DEVICES[entity_id].hass.bus.fire(
                        EVENT_BUTTON_PRESSED,
                        {
                            ATTR_ENTITY_ID: rfxtrx.RFX_DEVICES[entity_id].entity_id,
                            ATTR_STATE: event.values["Command"].lower(),
                        },
                    )

    # Subscribe to main rfxtrx events
    if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
        rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(light_update)


# Traceback (KeyError)
INFO:homeassistant.components.rfxtrx:Receive RFXCOM event from <class 'RFXtrx.LightingDevice'> type='LightwaveRF, Siemens' id='f11043:16' => f1104316 : 0a140005f1104310050050
INFO:homeassistant.components.switch.rfxtrx:Automatic add f1104316 rfxtrx.switch (Class: LightingDevice Sub: 0)
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: new_state=<state switch.f1104316__0a140005f1104310050050=off; friendly_name=f1104316 : 0a140005f1104310050050 @ 20:34:47 03-02-2016>, entity_id=switch.f1104316__0a140005f1104310050050>
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: new_state=<state group.all_switches=off; hidden=True, order=0, entity_id=('switch.f1104316__0a140005f1104310050050',), auto=True, friendly_name=all switches @ 20:34:47 03-02-2016>, entity_id=group.all_switches>
Traceback (most recent call last):
File "/usr/local/lib/python3.4/threading.py", line 911, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.4/threading.py", line 859, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.4/site-packages/RFXtrx/__init__.py", line 55, in _connect
self._event_callback(event)
File "/usr/src/app/homeassistant/components/rfxtrx.py", line 48, in handle_receive
subscriber(event)
File "/usr/src/app/homeassistant/components/switch/rfxtrx.py", line 81, in switch_update
event.values['Command']
KeyError: 'Command'

# Fixed code
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """Setup the RFXtrx platform."""
    import RFXtrx as rfxtrxmod

    lights = []
    devices = config.get("devices", None)

    if devices:
        for entity_id, entity_info in devices.items():
            if entity_id not in rfxtrx.RFX_DEVICES:
                _LOGGER.info("Add %s rfxtrx.light", entity_info[ATTR_NAME])

                # Check if i must fire event
                fire_event = entity_info.get(ATTR_FIREEVENT, False)
                datas = {ATTR_STATE: False, ATTR_FIREEVENT: fire_event}

                rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
                if not isinstance(rfxobject.device, rfxtrxmod.LightDevice):
                    _LOGGER.exception("%s is not a light", entity_info[ATTR_NAME])
                    return
                new_light = RfxtrxLight(entity_info[ATTR_NAME], rfxobject, datas)
                rfxtrx.RFX_DEVICES[entity_id] = new_light
                lights.append(new_light)

    add_devices_callback(lights)

    def light_update(event):
        """Callback for light updates from the RFXtrx gateway."""
        if not isinstance(event.device, rfxtrxmod.LightDevice):
            return

        # Add entity if not exist and the automatic_add is True
        entity_id = slugify(event.device.id_string.lower())
        if entity_id not in rfxtrx.RFX_DEVICES:
            automatic_add = config.get("automatic_add", False)
            if not automatic_add:
                return

            _LOGGER.info(
                "Automatic add %s rfxtrx.light (Class: %s Sub: %s)",
                entity_id,
                event.device.__class__.__name__,
                event.device.subtype,
            )
            pkt_id = "".join("{0:02x}".format(x) for x in event.data)
            entity_name = "%s : %s" % (entity_id, pkt_id)
            datas = {ATTR_STATE: False, ATTR_FIREEVENT: False}
            new_light = RfxtrxLight(entity_name, event, datas)
            rfxtrx.RFX_DEVICES[entity_id] = new_light
            add_devices_callback([new_light])

        # Check if entity exists or previously added automatically
        if entity_id in rfxtrx.RFX_DEVICES:
            _LOGGER.debug(
                "EntityID: %s light_update. Command: %s",
                entity_id,
                event.values["Command"],
            )

            if event.values["Command"] == "On" or event.values["Command"] == "Off":
                # Update the rfxtrx device state
                is_on = event.values["Command"] == "On"
                # pylint: disable=protected-access
                rfxtrx.RFX_DEVICES[entity_id]._state = is_on
                rfxtrx.RFX_DEVICES[entity_id].update_ha_state()

            elif event.values["Command"] == "Set level":
                # pylint: disable=protected-access
                rfxtrx.RFX_DEVICES[entity_id]._brightness = (
                    event.values["Dim level"] * 255 // 100
                )

                # Update the rfxtrx device state
                is_on = rfxtrx.RFX_DEVICES[entity_id]._brightness > 0
                rfxtrx.RFX_DEVICES[entity_id]._state = is_on
                rfxtrx.RFX_DEVICES[entity_id].update_ha_state()
            else:
                return

            # Fire event
            if rfxtrx.RFX_DEVICES[entity_id].should_fire_event:
                rfxtrx.RFX_DEVICES[entity_id].hass.bus.fire(
                    EVENT_BUTTON_PRESSED,
                    {
                        ATTR_ENTITY_ID: rfxtrx.RFX_DEVICES[entity_id].entity_id,
                        ATTR_STATE: event.values["Command"].lower(),
                    },
                )

    # Subscribe to main rfxtrx events
    if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
        rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(light_update)