From cb6c869c2fb97021b042feff38ea99d9b14d5e2d Mon Sep 17 00:00:00 2001 From: Mischa Gruber Date: Tue, 5 Jun 2018 18:15:34 +0200 Subject: [PATCH] Action parameter doesn't longer have to be the first parameter (#14815) * Action parameter doesn't longer have to be the first parameter * Minified code upon suggestion --- homeassistant/components/binary_sensor/mystrom.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/binary_sensor/mystrom.py b/homeassistant/components/binary_sensor/mystrom.py index 93d56a97c42..5c1d9a576a6 100644 --- a/homeassistant/components/binary_sensor/mystrom.py +++ b/homeassistant/components/binary_sensor/mystrom.py @@ -29,6 +29,7 @@ class MyStromView(HomeAssistantView): url = '/api/mystrom' name = 'api:mystrom' + supported_actions = ['single', 'double', 'long', 'touch'] def __init__(self, add_devices): """Initialize the myStrom URL endpoint.""" @@ -44,16 +45,18 @@ class MyStromView(HomeAssistantView): @asyncio.coroutine def _handle(self, hass, data): """Handle requests to the myStrom endpoint.""" - button_action = list(data.keys())[0] - button_id = data[button_action] - entity_id = '{}.{}_{}'.format(DOMAIN, button_id, button_action) + button_action = next(( + parameter for parameter in data + if parameter in self.supported_actions), None) - if button_action not in ['single', 'double', 'long', 'touch']: + if button_action is None: _LOGGER.error( "Received unidentified message from myStrom button: %s", data) return ("Received unidentified message: {}".format(data), HTTP_UNPROCESSABLE_ENTITY) + button_id = data[button_action] + entity_id = '{}.{}_{}'.format(DOMAIN, button_id, button_action) if entity_id not in self.buttons: _LOGGER.info("New myStrom button/action detected: %s/%s", button_id, button_action)