Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -8,8 +8,7 @@ from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up myStrom Binary Sensor."""
|
||||
hass.http.register_view(MyStromView(async_add_entities))
|
||||
|
||||
@@ -19,9 +18,9 @@ async def async_setup_platform(hass, config, async_add_entities,
|
||||
class MyStromView(HomeAssistantView):
|
||||
"""View to handle requests from myStrom buttons."""
|
||||
|
||||
url = '/api/mystrom'
|
||||
name = 'api:mystrom'
|
||||
supported_actions = ['single', 'double', 'long', 'touch']
|
||||
url = "/api/mystrom"
|
||||
name = "api:mystrom"
|
||||
supported_actions = ["single", "double", "long", "touch"]
|
||||
|
||||
def __init__(self, add_entities):
|
||||
"""Initialize the myStrom URL endpoint."""
|
||||
@@ -30,31 +29,35 @@ class MyStromView(HomeAssistantView):
|
||||
|
||||
async def get(self, request):
|
||||
"""Handle the GET request received from a myStrom button."""
|
||||
res = await self._handle(request.app['hass'], request.query)
|
||||
res = await self._handle(request.app["hass"], request.query)
|
||||
return res
|
||||
|
||||
async def _handle(self, hass, data):
|
||||
"""Handle requests to the myStrom endpoint."""
|
||||
button_action = next((
|
||||
parameter for parameter in data
|
||||
if parameter in self.supported_actions), None)
|
||||
button_action = next(
|
||||
(parameter for parameter in data if parameter in self.supported_actions),
|
||||
None,
|
||||
)
|
||||
|
||||
if button_action is None:
|
||||
_LOGGER.error(
|
||||
"Received unidentified message from myStrom button: %s", data)
|
||||
return ("Received unidentified message: {}".format(data),
|
||||
HTTP_UNPROCESSABLE_ENTITY)
|
||||
_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)
|
||||
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)
|
||||
_LOGGER.info(
|
||||
"New myStrom button/action detected: %s/%s", button_id, button_action
|
||||
)
|
||||
self.buttons[entity_id] = MyStromBinarySensor(
|
||||
'{}_{}'.format(button_id, button_action))
|
||||
"{}_{}".format(button_id, button_action)
|
||||
)
|
||||
self.add_entities([self.buttons[entity_id]])
|
||||
else:
|
||||
new_state = self.buttons[entity_id].state == 'off'
|
||||
new_state = self.buttons[entity_id].state == "off"
|
||||
self.buttons[entity_id].async_on_update(new_state)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user