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
This commit is contained in:
Mischa Gruber 2018-06-05 18:15:34 +02:00 committed by Fabian Affolter
parent 640e499964
commit cb6c869c2f

View file

@ -29,6 +29,7 @@ class MyStromView(HomeAssistantView):
url = '/api/mystrom' url = '/api/mystrom'
name = 'api:mystrom' name = 'api:mystrom'
supported_actions = ['single', 'double', 'long', 'touch']
def __init__(self, add_devices): def __init__(self, add_devices):
"""Initialize the myStrom URL endpoint.""" """Initialize the myStrom URL endpoint."""
@ -44,16 +45,18 @@ class MyStromView(HomeAssistantView):
@asyncio.coroutine @asyncio.coroutine
def _handle(self, hass, data): def _handle(self, hass, data):
"""Handle requests to the myStrom endpoint.""" """Handle requests to the myStrom endpoint."""
button_action = list(data.keys())[0] button_action = next((
button_id = data[button_action] parameter for parameter in data
entity_id = '{}.{}_{}'.format(DOMAIN, button_id, button_action) if parameter in self.supported_actions), None)
if button_action not in ['single', 'double', 'long', 'touch']: if button_action is None:
_LOGGER.error( _LOGGER.error(
"Received unidentified message from myStrom button: %s", data) "Received unidentified message from myStrom button: %s", data)
return ("Received unidentified message: {}".format(data), return ("Received unidentified message: {}".format(data),
HTTP_UNPROCESSABLE_ENTITY) HTTP_UNPROCESSABLE_ENTITY)
button_id = data[button_action]
entity_id = '{}.{}_{}'.format(DOMAIN, button_id, button_action)
if entity_id not in self.buttons: if entity_id not in self.buttons:
_LOGGER.info("New myStrom button/action detected: %s/%s", _LOGGER.info("New myStrom button/action detected: %s/%s",
button_id, button_action) button_id, button_action)