# Buggy code
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(),
                    },
                )


# 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 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(),
                },
            )