mirror of
https://github.com/home-assistant/core.git
synced 2025-12-27 14:31:13 +00:00
Add ability to set Zwave protection commandclass (#15390)
* Add API for protection commandclass * Adjusting * tests * Spelling * Missed flake8 * Period * spelling * Review changes * removing additional .keys() * period * Move i/o out into executor pool * Move i/o out into executor pool * Forgot get method * Do it right... I feel stupid * Long lines * Merging
This commit is contained in:
committed by
Paulus Schoutsen
parent
3204501174
commit
1b94fe3613
@@ -29,6 +29,7 @@ def async_setup(hass):
|
||||
hass.http.register_view(ZWaveUserCodeView)
|
||||
hass.http.register_view(ZWaveLogView)
|
||||
hass.http.register_view(ZWaveConfigWriteView)
|
||||
hass.http.register_view(ZWaveProtectionView)
|
||||
|
||||
return True
|
||||
|
||||
@@ -196,3 +197,59 @@ class ZWaveUserCodeView(HomeAssistantView):
|
||||
'label': value.label,
|
||||
'length': len(value.data)}
|
||||
return self.json(usercodes)
|
||||
|
||||
|
||||
class ZWaveProtectionView(HomeAssistantView):
|
||||
"""View for the protection commandclass of a node."""
|
||||
|
||||
url = r"/api/zwave/protection/{node_id:\d+}"
|
||||
name = "api:zwave:protection"
|
||||
|
||||
async def get(self, request, node_id):
|
||||
"""Retrieve the protection commandclass options of node."""
|
||||
nodeid = int(node_id)
|
||||
hass = request.app['hass']
|
||||
network = hass.data.get(const.DATA_NETWORK)
|
||||
|
||||
def _fetch_protection():
|
||||
"""Helper to get protection data."""
|
||||
node = network.nodes.get(nodeid)
|
||||
if node is None:
|
||||
return self.json_message('Node not found', HTTP_NOT_FOUND)
|
||||
protection_options = {}
|
||||
if not node.has_command_class(const.COMMAND_CLASS_PROTECTION):
|
||||
return self.json(protection_options)
|
||||
protections = node.get_protections()
|
||||
protection_options = {
|
||||
'value_id': '{0:d}'.format(list(protections)[0]),
|
||||
'selected': node.get_protection_item(list(protections)[0]),
|
||||
'options': node.get_protection_items(list(protections)[0])}
|
||||
return self.json(protection_options)
|
||||
|
||||
return await hass.async_add_executor_job(_fetch_protection)
|
||||
|
||||
async def post(self, request, node_id):
|
||||
"""Change the selected option in protection commandclass."""
|
||||
nodeid = int(node_id)
|
||||
hass = request.app['hass']
|
||||
network = hass.data.get(const.DATA_NETWORK)
|
||||
protection_data = await request.json()
|
||||
|
||||
def _set_protection():
|
||||
"""Helper to get protection data."""
|
||||
node = network.nodes.get(nodeid)
|
||||
selection = protection_data["selection"]
|
||||
value_id = int(protection_data[const.ATTR_VALUE_ID])
|
||||
if node is None:
|
||||
return self.json_message('Node not found', HTTP_NOT_FOUND)
|
||||
if not node.has_command_class(const.COMMAND_CLASS_PROTECTION):
|
||||
return self.json_message(
|
||||
'No protection commandclass on this node', HTTP_NOT_FOUND)
|
||||
state = node.set_protection(value_id, selection)
|
||||
if not state:
|
||||
return self.json_message(
|
||||
'Protection setting did not complete', 202)
|
||||
return self.json_message(
|
||||
'Protection setting succsessfully set', HTTP_OK)
|
||||
|
||||
return await hass.async_add_executor_job(_set_protection)
|
||||
|
||||
Reference in New Issue
Block a user