From b43ed169c70acc9c5ebc7e2a793d8d1e2ace1f91 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 5 Apr 2022 20:22:47 +0200 Subject: [PATCH] Added support for switches as native entities to zwave.mqtt plugin --- platypush/plugins/zwave/mqtt/__init__.py | 674 +++++++++++++++++------ 1 file changed, 509 insertions(+), 165 deletions(-) diff --git a/platypush/plugins/zwave/mqtt/__init__.py b/platypush/plugins/zwave/mqtt/__init__.py index 63279b900..772a046df 100644 --- a/platypush/plugins/zwave/mqtt/__init__.py +++ b/platypush/plugins/zwave/mqtt/__init__.py @@ -3,8 +3,9 @@ import queue from datetime import datetime from threading import Timer -from typing import Optional, List, Any, Dict, Union, Iterable, Callable +from typing import Optional, List, Any, Dict, Union, Iterable, Mapping, Callable +from platypush.entities.switches import Switch from platypush.message.event.zwave import ZwaveNodeRenamedEvent, ZwaveNodeEvent from platypush.context import get_backend, get_bus @@ -45,10 +46,21 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): """ - def __init__(self, name: str, host: str = 'localhost', port: int = 1883, topic_prefix: str = 'zwave', - timeout: int = 10, tls_certfile: Optional[str] = None, tls_keyfile: Optional[str] = None, - tls_version: Optional[str] = None, tls_ciphers: Optional[str] = None, username: Optional[str] = None, - password: Optional[str] = None, **kwargs): + def __init__( + self, + name: str, + host: str = 'localhost', + port: int = 1883, + topic_prefix: str = 'zwave', + timeout: int = 10, + tls_certfile: Optional[str] = None, + tls_keyfile: Optional[str] = None, + tls_version: Optional[str] = None, + tls_ciphers: Optional[str] = None, + username: Optional[str] = None, + password: Optional[str] = None, + **kwargs, + ): """ :param name: Gateway name, as configured from the zwavejs2mqtt web panel from Mqtt -> Name. :param host: MQTT broker host, as configured from the zwavejs2mqtt web panel from Mqtt -> Host @@ -70,9 +82,17 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param password: If the connection requires user authentication, specify the password (default: None) """ - super().__init__(host=host, port=port, tls_certfile=tls_certfile, tls_keyfile=tls_keyfile, - tls_version=tls_version, tls_ciphers=tls_ciphers, username=username, - password=password, **kwargs) + super().__init__( + host=host, + port=port, + tls_certfile=tls_certfile, + tls_keyfile=tls_keyfile, + tls_version=tls_version, + tls_ciphers=tls_ciphers, + username=username, + password=password, + **kwargs, + ) self.topic_prefix = topic_prefix self.base_topic = topic_prefix + '/{}/ZWAVE_GATEWAY-' + name @@ -118,14 +138,21 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): if isinstance(response, Response): response = response.output - assert response.get('success') is True, response.get('message', 'zwavejs2mqtt error') + assert response.get('success') is True, response.get( + 'message', 'zwavejs2mqtt error' + ) return response def _api_request(self, api: str, *args, **kwargs): payload = json.dumps({'args': args}) ret = self._parse_response( - self.publish(topic=self._api_topic(api) + '/set', msg=payload, reply_topic=self._api_topic(api), - **self._mqtt_args(**kwargs))) + self.publish( + topic=self._api_topic(api) + '/set', + msg=payload, + reply_topic=self._api_topic(api), + **self._mqtt_args(**kwargs), + ) + ) assert not ret or ret.get('success') is True, ret.get('message') return ret.get('result') @@ -135,7 +162,12 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): if t: return datetime.fromtimestamp(t / 1000) - def _get_scene(self, scene_id: Optional[int] = None, scene_label: Optional[str] = None, **kwargs) -> dict: + def _get_scene( + self, + scene_id: Optional[int] = None, + scene_label: Optional[str] = None, + **kwargs, + ) -> dict: assert scene_id or scene_label, 'No scene_id/scene_label specified' if scene_id in self._scenes_cache['by_id']: return self._scenes_cache['by_id'][scene_id] @@ -191,8 +223,10 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): 'id_on_network': value['id'], 'value_id': value['id'], 'data': value.get('value'), - 'data_items': [state for state in value['states']] if len(value.get('states', [])) > 1 else None, - 'label': value.get('label', value.get('propertyName', value.get('property'))), + 'data_items': value['states'] if len(value.get('states', [])) > 1 else None, + 'label': value.get( + 'label', value.get('propertyName', value.get('property')) + ), 'property_id': value.get('property'), 'help': value.get('description'), 'node_id': value.get('nodeId'), @@ -209,8 +243,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): 'is_read_only': value['readable'] and not value['writeable'], 'is_write_only': value['writeable'] and not value['readable'], 'last_update': cls._convert_timestamp(value.get('lastUpdate')), - **({'property_key': value['propertyKey']} if 'propertyKey' in value else {}), - **({'property_key_name': value['propertyKeyName']} if 'propertyKeyName' in value else {}), + **( + {'property_key': value['propertyKey']} if 'propertyKey' in value else {} + ), + **( + {'property_key_name': value['propertyKeyName']} + if 'propertyKeyName' in value + else {} + ), } @staticmethod @@ -250,7 +290,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): 'device_id': node['hexId'].replace('0x', ''), 'name': node.get('name'), 'capabilities': capabilities, - 'manufacturer_id': '0x{:04x}'.format(node['manufacturerId']) if node.get('manufacturerId') else None, + 'manufacturer_id': '0x{:04x}'.format(node['manufacturerId']) + if node.get('manufacturerId') + else None, 'manufacturer_name': node.get('manufacturer'), 'location': node.get('loc'), 'status': node.get('status'), @@ -268,9 +310,15 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): 'is_security_device': node.get('supportsSecurity'), 'is_sleeping': node.get('ready') and node.get('status') == 'Asleep', 'last_update': cls._convert_timestamp(node.get('lastActive')), - 'product_id': '0x{:04x}'.format(node['productId']) if node.get('productId') else None, - 'product_type': '0x{:04x}'.format(node['productType']) if node.get('productType') else None, - 'product_name': ' '.join([node.get('productLabel', ''), node.get('productDescription', '')]), + 'product_id': '0x{:04x}'.format(node['productId']) + if node.get('productId') + else None, + 'product_type': '0x{:04x}'.format(node['productType']) + if node.get('productType') + else None, + 'product_name': ' '.join( + [node.get('productLabel', ''), node.get('productDescription', '')] + ), 'baud_rate': node.get('dataRate'), 'max_baud_rate': node.get('maxBaudRate', node.get('dataRate')), 'device_class': node.get('deviceClass'), @@ -284,25 +332,28 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): 'zwave_plus_node_type': node.get('zwavePlusNodeType'), 'zwave_plus_role_type': node.get('zwavePlusRoleType'), 'neighbours': node.get('neighbors', []), - 'command_classes': { value['commandClassName'] - for value in node.get('values', {}).values() if value.get('commandClassName') + for value in node.get('values', {}).values() + if value.get('commandClassName') }, - 'groups': { group['value']: cls.group_to_dict(group, node_id=node['id']) for group in node.get('groups', []) }, - 'values': { value['id']: cls.value_to_dict(value) for value in node.get('values', {}).values() }, } - def _get_node(self, node_id: Optional[int] = None, node_name: Optional[str] = None, use_cache: bool = True, - **kwargs) -> Optional[dict]: + def _get_node( + self, + node_id: Optional[int] = None, + node_name: Optional[str] = None, + use_cache: bool = True, + **kwargs, + ) -> Optional[dict]: assert node_id or node_name, 'Please provide either a node_id or node_name' if use_cache: if node_id and node_id in self._nodes_cache['by_id']: @@ -335,12 +386,21 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): return node - def _get_value(self, value_id: Optional[int] = None, id_on_network: Optional[str] = None, - value_label: Optional[str] = None, node_id: Optional[int] = None, node_name: Optional[str] = None, - use_cache: bool = True, **_) -> Dict[str, Any]: + def _get_value( + self, + value_id: Optional[int] = None, + id_on_network: Optional[str] = None, + value_label: Optional[str] = None, + node_id: Optional[int] = None, + node_name: Optional[str] = None, + use_cache: bool = True, + **_, + ) -> Dict[str, Any]: # Unlike python-openzwave, value_id and id_on_network are the same on zwavejs2mqtt value_id = value_id or id_on_network - assert value_id or value_label, 'Please provide either value_id, id_on_network or value_label' + assert ( + value_id or value_label + ), 'Please provide either value_id, id_on_network or value_label' if use_cache: if value_id and value_id in self._values_cache['by_id']: @@ -350,7 +410,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): nodes = [] if node_id or node_name: - nodes = [self._get_node(node_id=node_id, node_name=node_name, use_cache=False)] + nodes = [ + self._get_node(node_id=node_id, node_name=node_name, use_cache=False) + ] if not nodes: # noinspection PyUnresolvedReferences @@ -376,8 +438,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): value = values[0] if value.get('property_id') == 'targetValue': - cur_value_id = '-'.join(value['value_id'].split('-')[:-1] + ['currentValue']) - cur_value = self._nodes_cache['by_id'][value['node_id']].get('values', {}).get(cur_value_id) + cur_value_id = '-'.join( + value['value_id'].split('-')[:-1] + ['currentValue'] + ) + cur_value = ( + self._nodes_cache['by_id'][value['node_id']] + .get('values', {}) + .get(cur_value_id) + ) if cur_value: value['data'] = cur_value['data'] @@ -385,20 +453,59 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): if value['label']: self._values_cache['by_label'][value['label']] = value + self.publish_entities([value]) # type: ignore return value + @staticmethod + def _is_switch(value: Mapping): + return value.get('command_class_name', '').endswith('Switch') + + def transform_entities(self, values: Iterable[Mapping]): + entities = [] + + for value in values: + if self._is_switch(value): + entities.append( + Switch( + id=value['id'], + name='{node_name} [{value_name}]'.format( + node_name=self._nodes_cache['by_id'][value['node_id']].get( + 'name', f'[Node {value["node_id"]}]' + ), + value_name=value["label"], + ), + state=value['data'], + data={ + 'help': value.get('help'), + 'is_read_only': value.get('is_read_only'), + 'is_write_only': value.get('is_write_only'), + 'label': value.get('label'), + 'node_id': value.get('node_id'), + }, + ) + ) + + return super().transform_entities(entities) # type: ignore + def _topic_by_value_id(self, value_id: str) -> str: return self.topic_prefix + '/' + '/'.join(value_id.split('-')) - def _filter_values(self, command_classes: Iterable[str], filter_callback: Optional[Callable[[dict], bool]] = None, - node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) -> Dict[str, Any]: - # noinspection PyUnresolvedReferences - nodes = [self._get_node(node_name=node_name, use_cache=False, **kwargs)] if node_id or node_name else \ - self.get_nodes(**kwargs).output.values() + def _filter_values( + self, + command_classes: Iterable[str], + filter_callback: Optional[Callable[[dict], bool]] = None, + node_id: Optional[int] = None, + node_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: + nodes = ( + [self._get_node(node_name=node_name, use_cache=False, **kwargs)] + if node_id or node_name + else self.get_nodes(**kwargs).output.values() + ) command_classes = { - command_class_by_name[command_name] - for command_name in command_classes + command_class_by_name[command_name] for command_name in command_classes } values = {} @@ -406,21 +513,32 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): for node in nodes: for value in node.get('values', {}).values(): if value.get('command_class') not in command_classes or ( - filter_callback and not filter_callback(value)): + filter_callback and not filter_callback(value) + ): continue value_id = value['id_on_network'] if value_id.split('-').pop() == 'targetValue': value_id = '-'.join(value_id.split('-')[:-1]) + '-currentValue' - cur_value = self._nodes_cache['by_id'][value['node_id']].get('values', {}).get(value_id) + cur_value = ( + self._nodes_cache['by_id'][value['node_id']] + .get('values', {}) + .get(value_id) + ) if cur_value: value['data'] = cur_value['data'] values[value['id_on_network']] = value + self.publish_entities(values.values()) # type: ignore return values - def _get_group(self, group_id: Optional[str] = None, group_index: Optional[int] = None, **kwargs) -> dict: + def _get_group( + self, + group_id: Optional[str] = None, + group_index: Optional[int] = None, + **kwargs, + ) -> dict: group_id = group_id or group_index assert group_id is not None, 'No group_id/group_index specified' group = self._groups_cache.get(group_id) @@ -465,14 +583,19 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): msg_queue.put(json.loads(msg.payload)) client.on_message = on_message - client.connect(kwargs.get('host', self.host), kwargs.get('port', self.port), - keepalive=kwargs.get('timeout', self.timeout)) + client.connect( + kwargs.get('host', self.host), + kwargs.get('port', self.port), + keepalive=kwargs.get('timeout', self.timeout), + ) client.subscribe(topic) client.loop_start() try: - status = msg_queue.get(block=True, timeout=kwargs.get('timeout', self.timeout)) + status = msg_queue.get( + block=True, timeout=kwargs.get('timeout', self.timeout) + ) except queue.Empty: raise TimeoutError('The request timed out') finally: @@ -510,7 +633,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): raise _NOT_IMPLEMENTED_ERR @action - def remove_failed_node(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs): + def remove_failed_node( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ): """ Remove a failed node from the network. @@ -526,7 +651,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self._api_request('removeFailedNode', node_id, **kwargs) @action - def replace_failed_node(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs): + def replace_failed_node( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ): """ Replace a failed node on the network. @@ -549,7 +676,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): raise _NOT_IMPLEMENTED_ERR @action - def request_network_update(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs): + def request_network_update( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ): """ Request a network update to a node. @@ -575,8 +704,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self._api_request('refreshNeighbors', **kwargs) @action - def get_nodes(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) \ - -> Optional[Dict[str, Any]]: + def get_nodes( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Optional[Dict[str, Any]]: """ Get the nodes associated to the network. @@ -740,7 +870,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): (default: query the default configured device). """ if node_id or node_name: - return self._get_node(node_id=node_id, node_name=node_name, use_cache=False, **kwargs) + return self._get_node( + node_id=node_id, node_name=node_name, use_cache=False, **kwargs + ) nodes = { node['id']: self.node_to_dict(node) @@ -748,10 +880,7 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): } self._nodes_cache['by_id'] = nodes - self._nodes_cache['by_name'] = { - node['name']: node - for node in nodes.values() - } + self._nodes_cache['by_name'] = {node['name']: node for node in nodes.values()} return nodes @@ -763,7 +892,13 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): raise _NOT_IMPLEMENTED_ERR @action - def set_node_name(self, new_name: str, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs): + def set_node_name( + self, + new_name: str, + node_id: Optional[int] = None, + node_name: Optional[str] = None, + **kwargs, + ): """ Rename a node on the network. @@ -778,10 +913,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): assert node_id, f'No such node: {node_id}' self._api_request('setNodeName', node_id, new_name, **kwargs) - get_bus().post(ZwaveNodeRenamedEvent(node={ - **self._get_node(node_id=node_id), - 'name': new_name, - })) + get_bus().post( + ZwaveNodeRenamedEvent( + node={ + **self._get_node(node_id=node_id), + 'name': new_name, + } + ) + ) @action def set_node_product_name(self, **kwargs): @@ -798,8 +937,13 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): raise _NOT_IMPLEMENTED_ERR @action - def set_node_location(self, location: str, node_id: Optional[int] = None, node_name: Optional[str] = None, - **kwargs): + def set_node_location( + self, + location: str, + node_id: Optional[int] = None, + node_name: Optional[str] = None, + **kwargs, + ): """ Set the location of a node. @@ -814,10 +958,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): assert node_id, f'No such node: {node_id}' self._api_request('setNodeLocation', node_id, location, **kwargs) - get_bus().post(ZwaveNodeEvent(node={ - **self._get_node(node_id=node_id), - 'location': location, - })) + get_bus().post( + ZwaveNodeEvent( + node={ + **self._get_node(node_id=node_id), + 'location': location, + } + ) + ) @action def cancel_command(self, **kwargs): @@ -877,7 +1025,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): """ self._api_request('beginHealingNetwork', **kwargs) if timeout: - Timer(timeout, lambda: self._api_request('stopHealingNetwork', **kwargs)).start() + Timer( + timeout, lambda: self._api_request('stopHealingNetwork', **kwargs) + ).start() @action def switch_all(self, **kwargs): @@ -894,9 +1044,15 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): raise _NOT_IMPLEMENTED_ERR @action - def get_value(self, value_id: Optional[int] = None, id_on_network: Optional[str] = None, - value_label: Optional[str] = None, node_id: Optional[int] = None, node_name: Optional[str] = None, - **kwargs) -> Dict[str, Any]: + def get_value( + self, + value_id: Optional[int] = None, + id_on_network: Optional[str] = None, + value_label: Optional[str] = None, + node_id: Optional[int] = None, + node_name: Optional[str] = None, + **kwargs, + ) -> Dict[str, Any]: """ Get a value on the network. @@ -908,13 +1064,27 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._get_value(value_id=value_id, value_label=value_label, id_on_network=id_on_network, node_id=node_id, - node_name=node_name, use_cache=False, **kwargs) + return self._get_value( + value_id=value_id, + value_label=value_label, + id_on_network=id_on_network, + node_id=node_id, + node_name=node_name, + use_cache=False, + **kwargs, + ) @action - def set_value(self, data, value_id: Optional[int] = None, id_on_network: Optional[str] = None, - value_label: Optional[str] = None, node_id: Optional[int] = None, node_name: Optional[str] = None, - **kwargs): + def set_value( + self, + data, + value_id: Optional[int] = None, + id_on_network: Optional[str] = None, + value_label: Optional[str] = None, + node_id: Optional[int] = None, + node_name: Optional[str] = None, + **kwargs, + ): """ Set a value. @@ -927,16 +1097,31 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - value = self._get_value(value_id=value_id, value_label=value_label, id_on_network=id_on_network, - node_id=node_id, node_name=node_name, **kwargs) + value = self._get_value( + value_id=value_id, + value_label=value_label, + id_on_network=id_on_network, + node_id=node_id, + node_name=node_name, + **kwargs, + ) - self._api_request('writeValue', { - 'nodeId': value['node_id'], - 'commandClass': value['command_class'], - 'endpoint': value.get('endpoint', 0), - 'property': value['property_id'], - **({'propertyKey': value['property_key']} if 'property_key' in value else {}), - }, data, **kwargs) + self._api_request( + 'writeValue', + { + 'nodeId': value['node_id'], + 'commandClass': value['command_class'], + 'endpoint': value.get('endpoint', 0), + 'property': value['property_id'], + **( + {'propertyKey': value['property_key']} + if 'property_key' in value + else {} + ), + }, + data, + **kwargs, + ) @action def set_value_label(self, **kwargs): @@ -960,7 +1145,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): raise _NOT_IMPLEMENTED_ERR @action - def node_heal(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs): + def node_heal( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ): """ Heal network node by requesting the node to rediscover their neighbours. @@ -974,7 +1161,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self._api_request('healNode', node_id, **kwargs) @action - def node_update_neighbours(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs): + def node_update_neighbours( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ): """ Ask a node to update its neighbours table (same as :meth:`platypush.plugins.zwave.mqtt.ZwaveMqttPlugin.node_heal`). @@ -987,7 +1176,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self.node_heal(node_id=node_id, node_name=node_name, **kwargs) @action - def node_network_update(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs): + def node_network_update( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ): """ Update the controller with network information (same as :meth:`platypush.plugins.zwave.mqtt.ZwaveMqttPlugin.node_heal`). @@ -1000,7 +1191,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self.node_heal(node_id=node_id, node_name=node_name, **kwargs) @action - def node_refresh_info(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs): + def node_refresh_info( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ): """ Fetch up-to-date information about the node. @@ -1014,7 +1207,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self._api_request('refreshInfo', node_id, **kwargs) @action - def get_dimmers(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) -> Dict[str, Any]: + def get_dimmers( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the dimmers on the network or associated to a node. @@ -1023,12 +1218,17 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['switch_multilevel', 'switch_toggle_multilevel'], node_id=node_id, - node_name=node_name, **kwargs) + return self._filter_values( + ['switch_multilevel', 'switch_toggle_multilevel'], + node_id=node_id, + node_name=node_name, + **kwargs, + ) @action - def get_node_config(self, node_id: Optional[int] = None, node_name: Optional[str] = None, - **kwargs) -> Dict[str, Any]: + def get_node_config( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the configuration values of a node or of all the nodes on the network. @@ -1037,12 +1237,22 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['configuration', 'ip_configuration', 'association_command_configuration', - 'sensor_configuration'], node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + [ + 'configuration', + 'ip_configuration', + 'association_command_configuration', + 'sensor_configuration', + ], + node_id=node_id, + node_name=node_name, + **kwargs, + ) @action - def get_battery_levels(self, node_id: Optional[int] = None, node_name: Optional[str] = None, - **kwargs) -> Dict[str, Any]: + def get_battery_levels( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the battery levels of a node or of all the nodes on the network. @@ -1051,11 +1261,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['battery'], node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + ['battery'], node_id=node_id, node_name=node_name, **kwargs + ) @action - def get_power_levels(self, node_id: Optional[int] = None, node_name: Optional[str] = None, - **kwargs) -> Dict[str, Any]: + def get_power_levels( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the power levels of this node. @@ -1064,10 +1277,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['powerlevel'], node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + ['powerlevel'], node_id=node_id, node_name=node_name, **kwargs + ) @action - def get_bulbs(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) -> Dict[str, Any]: + def get_bulbs( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the bulbs/LEDs on the network or associated to a node. @@ -1076,11 +1293,18 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['color'], filter_callback=lambda value: not value['is_read_only'], node_id=node_id, - node_name=node_name, **kwargs) + return self._filter_values( + ['color'], + filter_callback=lambda value: not value['is_read_only'], + node_id=node_id, + node_name=node_name, + **kwargs, + ) @action - def get_switches(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) -> Dict[str, Any]: + def get_switches( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the switches on the network or associated to a node. @@ -1089,12 +1313,18 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['switch_binary', 'switch_toggle_binary'], - filter_callback=lambda value: not value['is_read_only'], - node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + ['switch_binary', 'switch_toggle_binary'], + filter_callback=lambda value: not value['is_read_only'], + node_id=node_id, + node_name=node_name, + **kwargs, + ) @action - def get_sensors(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) -> Dict[str, Any]: + def get_sensors( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the sensors on the network or associated to a node. @@ -1103,12 +1333,18 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['sensor_binary', 'sensor_multilevel', 'sensor_alarm', 'meter'], - filter_callback=lambda value: not value['is_write_only'], - node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + ['sensor_binary', 'sensor_multilevel', 'sensor_alarm', 'meter'], + filter_callback=lambda value: not value['is_write_only'], + node_id=node_id, + node_name=node_name, + **kwargs, + ) @action - def get_doorlocks(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) -> Dict[str, Any]: + def get_doorlocks( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the doorlocks on the network or associated to a node. @@ -1117,10 +1353,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['door_lock'], node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + ['door_lock'], node_id=node_id, node_name=node_name, **kwargs + ) @action - def get_locks(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) -> Dict[str, Any]: + def get_locks( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the locks on the network or associated to a node. @@ -1129,10 +1369,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['lock'], node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + ['lock'], node_id=node_id, node_name=node_name, **kwargs + ) @action - def get_usercodes(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) -> Dict[str, Any]: + def get_usercodes( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the usercodes on the network or associated to a node. @@ -1141,11 +1385,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['user_code'], node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + ['user_code'], node_id=node_id, node_name=node_name, **kwargs + ) @action - def get_thermostats(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) \ - -> Dict[str, Any]: + def get_thermostats( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the thermostats on the network or associated to a node. @@ -1154,13 +1401,25 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['thermostat_heating', 'thermostat_mode', 'thermostat_operating_state', - 'thermostat_setpoint', 'thermostat_fan_mode', 'thermostat_fan_state', - 'thermostat_setback'], node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + [ + 'thermostat_heating', + 'thermostat_mode', + 'thermostat_operating_state', + 'thermostat_setpoint', + 'thermostat_fan_mode', + 'thermostat_fan_state', + 'thermostat_setback', + ], + node_id=node_id, + node_name=node_name, + **kwargs, + ) @action - def get_protections(self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs) \ - -> Dict[str, Any]: + def get_protections( + self, node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs + ) -> Dict[str, Any]: """ Get the protection-compatible devices on the network or associated to a node. @@ -1169,7 +1428,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - return self._filter_values(['protection'], node_id=node_id, node_name=node_name, **kwargs) + return self._filter_values( + ['protection'], node_id=node_id, node_name=node_name, **kwargs + ) @action def get_groups(self, **kwargs) -> Dict[str, dict]: @@ -1218,7 +1479,10 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): group['group_id']: { **group, 'associations': [ - assoc['nodeId'] for assoc in self._api_request('getAssociations', node_id, group['index']) + assoc['nodeId'] + for assoc in self._api_request( + 'getAssociations', node_id, group['index'] + ) ], } for node_id, node in nodes.items() @@ -1275,7 +1539,12 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self._api_request('_createScene', label) @action - def remove_scene(self, scene_id: Optional[int] = None, scene_label: Optional[str] = None, **kwargs): + def remove_scene( + self, + scene_id: Optional[int] = None, + scene_label: Optional[str] = None, + **kwargs, + ): """ Remove a scene. @@ -1288,7 +1557,12 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self._api_request('_removeScene', scene['scene_id']) @action - def activate_scene(self, scene_id: Optional[int] = None, scene_label: Optional[str] = None, **kwargs): + def activate_scene( + self, + scene_id: Optional[int] = None, + scene_label: Optional[str] = None, + **kwargs, + ): """ Activate a scene. @@ -1301,8 +1575,13 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self._api_request('_activateScene', scene['scene_id']) @action - def set_scene_label(self, new_label: str, scene_id: Optional[int] = None, scene_label: Optional[str] = None, - **kwargs): + def set_scene_label( + self, + new_label: str, + scene_id: Optional[int] = None, + scene_label: Optional[str] = None, + **kwargs, + ): """ Rename a scene/set the scene label. @@ -1315,10 +1594,18 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): raise _NOT_IMPLEMENTED_ERR @action - def scene_add_value(self, data: Optional[Any] = None, value_id: Optional[int] = None, - id_on_network: Optional[str] = None, value_label: Optional[str] = None, - scene_id: Optional[int] = None, scene_label: Optional[str] = None, - node_id: Optional[int] = None, node_name: Optional[str] = None, **kwargs): + def scene_add_value( + self, + data: Optional[Any] = None, + value_id: Optional[int] = None, + id_on_network: Optional[str] = None, + value_label: Optional[str] = None, + scene_id: Optional[int] = None, + scene_label: Optional[str] = None, + node_id: Optional[int] = None, + node_name: Optional[str] = None, + **kwargs, + ): """ Add a value to a scene. @@ -1333,22 +1620,41 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - value = self._get_value(value_id=value_id, value_label=value_label, id_on_network=id_on_network, - node_id=node_id, node_name=node_name, **kwargs) + value = self._get_value( + value_id=value_id, + value_label=value_label, + id_on_network=id_on_network, + node_id=node_id, + node_name=node_name, + **kwargs, + ) scene = self._get_scene(scene_id=scene_id, scene_label=scene_label, **kwargs) - self._api_request('_addSceneValue', scene['scene_id'], { - 'nodeId': value['node_id'], - 'commandClass': value['command_class'], - 'property': value['property_id'], - 'endpoint': value['endpoint'], - }, data, kwargs.get('timeout', self.timeout)) + self._api_request( + '_addSceneValue', + scene['scene_id'], + { + 'nodeId': value['node_id'], + 'commandClass': value['command_class'], + 'property': value['property_id'], + 'endpoint': value['endpoint'], + }, + data, + kwargs.get('timeout', self.timeout), + ) @action - def scene_remove_value(self, value_id: Optional[int] = None, id_on_network: Optional[str] = None, - value_label: Optional[str] = None, scene_id: Optional[int] = None, - scene_label: Optional[str] = None, node_id: Optional[int] = None, - node_name: Optional[str] = None, **kwargs): + def scene_remove_value( + self, + value_id: Optional[int] = None, + id_on_network: Optional[str] = None, + value_label: Optional[str] = None, + scene_id: Optional[int] = None, + scene_label: Optional[str] = None, + node_id: Optional[int] = None, + node_name: Optional[str] = None, + **kwargs, + ): """ Remove a value from a scene. @@ -1362,13 +1668,24 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): :param kwargs: Extra arguments to be passed to :meth:`platypush.plugins.mqtt.MqttPlugin.publish`` (default: query the default configured device). """ - value = self._get_value(value_id=value_id, value_label=value_label, id_on_network=id_on_network, - node_id=node_id, node_name=node_name, **kwargs) + value = self._get_value( + value_id=value_id, + value_label=value_label, + id_on_network=id_on_network, + node_id=node_id, + node_name=node_name, + **kwargs, + ) scene = self._get_scene(scene_id=scene_id, scene_label=scene_label, **kwargs) self._api_request('_removeSceneValue', scene['scene_id'], value['value_id']) @action - def get_scene_values(self, scene_id: Optional[int] = None, scene_label: Optional[str] = None, **kwargs) -> dict: + def get_scene_values( + self, + scene_id: Optional[int] = None, + scene_label: Optional[str] = None, + **kwargs, + ) -> dict: """ Get the values associated to a scene. @@ -1381,8 +1698,13 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): return scene.get('values', {}) @action - def create_button(self, button_id: Union[int, str], node_id: Optional[int] = None, node_name: Optional[str] = None, - **kwargs): + def create_button( + self, + button_id: Union[int, str], + node_id: Optional[int] = None, + node_name: Optional[str] = None, + **kwargs, + ): """ Create a handheld button on a device. Only intended for bridge firmware controllers (not implemented by zwavejs2mqtt). @@ -1396,8 +1718,13 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): raise _NOT_IMPLEMENTED_ERR @action - def delete_button(self, button_id: Union[int, str], node_id: Optional[int] = None, node_name: Optional[str] = None, - **kwargs): + def delete_button( + self, + button_id: Union[int, str], + node_id: Optional[int] = None, + node_name: Optional[str] = None, + **kwargs, + ): """ Delete a button association from a device. Only intended for bridge firmware controllers. (not implemented by zwavejs2mqtt). @@ -1411,8 +1738,13 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): raise _NOT_IMPLEMENTED_ERR @action - def add_node_to_group(self, group_id: Optional[str] = None, node_id: Optional[int] = None, - endpoint: Optional[int] = None, **kwargs): + def add_node_to_group( + self, + group_id: Optional[str] = None, + node_id: Optional[int] = None, + endpoint: Optional[int] = None, + **kwargs, + ): """ Add a node to a group. @@ -1430,8 +1762,13 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self._api_request('addAssociations', group['node_id'], group['index'], [assoc]) @action - def remove_node_from_group(self, group_id: Optional[str] = None, node_id: Optional[int] = None, - endpoint: Optional[int] = None, **kwargs): + def remove_node_from_group( + self, + group_id: Optional[str] = None, + node_id: Optional[int] = None, + endpoint: Optional[int] = None, + **kwargs, + ): """ Remove a node from a group. @@ -1446,7 +1783,9 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): if endpoint is not None: assoc['endpoint'] = endpoint - self._api_request('removeAssociations', group['node_id'], group['index'], [assoc]) + self._api_request( + 'removeAssociations', group['node_id'], group['index'], [assoc] + ) @action def create_new_primary(self, **kwargs): @@ -1527,8 +1866,10 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): self.set_value(data=value['data'], id_on_network=device, **kwargs) return { - 'name': '{} - {}'.format(self._nodes_cache['by_id'][value['node_id']]['name'], - value.get('label', '[No Label]')), + 'name': '{} - {}'.format( + self._nodes_cache['by_id'][value['node_id']]['name'], + value.get('label', '[No Label]'), + ), 'on': value['data'], 'id': value['value_id'], } @@ -1541,11 +1882,14 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin): devices = self.get_switches().output.values() return [ { - 'name': '{} - {}'.format(self._nodes_cache['by_id'][dev['node_id']]['name'], - dev.get('label', '[No Label]')), + 'name': '{} - {}'.format( + self._nodes_cache['by_id'][dev['node_id']]['name'], + dev.get('label', '[No Label]'), + ), 'on': dev['data'], 'id': dev['value_id'], - } for dev in devices + } + for dev in devices ]