A more robust logic to parse `zwave.mqtt` value attributes.

This commit is contained in:
Fabio Manganiello 2022-12-10 16:21:29 +01:00
parent c4f649a0d5
commit cf9d34d38e
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
1 changed files with 16 additions and 4 deletions

View File

@ -264,7 +264,7 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin):
@classmethod @classmethod
def _get_type(cls, value: dict) -> str: def _get_type(cls, value: dict) -> str:
if value['list']: if value.get('list'):
if len(value.get('states', [])) > 1: if len(value.get('states', [])) > 1:
return 'List' return 'List'
if value.get('min') is not None and value.get('max') is not None: if value.get('min') is not None and value.get('max') is not None:
@ -279,6 +279,18 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin):
@classmethod @classmethod
def value_to_dict(cls, value: dict) -> dict: def value_to_dict(cls, value: dict) -> dict:
is_read_only = (
value['is_read_only']
if 'is_read_only' in value
else value.get('readable') and not value.get('writeable')
)
is_write_only = (
value['is_write_only']
if 'is_write_only' in value
else not value.get('readable') and value.get('writeable')
)
return { return {
'id': value['id'], 'id': value['id'],
'id_on_network': value['id'], 'id_on_network': value['id'],
@ -289,7 +301,7 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin):
'label', value.get('propertyName', value.get('property')) 'label', value.get('propertyName', value.get('property'))
), ),
'property_id': value.get('property'), 'property_id': value.get('property'),
'help': value.get('description'), 'help': value.get('description', value.get('help')),
'node_id': value.get('nodeId'), 'node_id': value.get('nodeId'),
'parent_id': value.get('nodeId'), 'parent_id': value.get('nodeId'),
'type': cls._get_type(value), 'type': cls._get_type(value),
@ -301,8 +313,8 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin):
'units': value.get('unit'), 'units': value.get('unit'),
'min': value.get('min'), 'min': value.get('min'),
'max': value.get('max'), 'max': value.get('max'),
'is_read_only': value['readable'] and not value['writeable'], 'is_read_only': is_read_only,
'is_write_only': value['writeable'] and not value['readable'], 'is_write_only': is_write_only,
'last_update': cls._convert_timestamp(value.get('lastUpdate')), 'last_update': cls._convert_timestamp(value.get('lastUpdate')),
**( **(
{'property_key': value['propertyKey']} if 'propertyKey' in value else {} {'property_key': value['propertyKey']} if 'propertyKey' in value else {}