Skip to content

Commit

Permalink
Added possibility to define the data type of Homematic (#24078)
Browse files Browse the repository at this point in the history
* Homematic: Added possibility to define the data type for set_device_value

* Fixed coding style

* Fixed variable name
  • Loading branch information
p0l0 authored and pvizeli committed May 24, 2019
1 parent 0a9a8ec commit ca2a682
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion homeassistant/components/homematic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Support for HomeMatic devices."""
from datetime import timedelta
from datetime import timedelta, datetime
from functools import partial
import logging

Expand Down Expand Up @@ -34,6 +34,7 @@
ATTR_CHANNEL = 'channel'
ATTR_ADDRESS = 'address'
ATTR_VALUE = 'value'
ATTR_VALUE_TYPE = 'value_type'
ATTR_INTERFACE = 'interface'
ATTR_ERRORCODE = 'error'
ATTR_MESSAGE = 'message'
Expand Down Expand Up @@ -235,6 +236,10 @@
vol.Required(ATTR_CHANNEL): vol.Coerce(int),
vol.Required(ATTR_PARAM): vol.All(cv.string, vol.Upper),
vol.Required(ATTR_VALUE): cv.match_all,
vol.Optional(ATTR_VALUE_TYPE): vol.In([
'boolean', 'dateTime.iso8601',
'double', 'int', 'string'
]),
vol.Optional(ATTR_INTERFACE): cv.string,
})

Expand Down Expand Up @@ -379,6 +384,22 @@ def _service_handle_device(service):
channel = service.data.get(ATTR_CHANNEL)
param = service.data.get(ATTR_PARAM)
value = service.data.get(ATTR_VALUE)
value_type = service.data.get(ATTR_VALUE_TYPE)

# Convert value into correct XML-RPC Type.
# https://docs.python.org/3/library/xmlrpc.client.html#xmlrpc.client.ServerProxy
if value_type:
if value_type == 'int':
value = int(value)
elif value_type == 'double':
value = float(value)
elif value_type == 'boolean':
value = bool(value)
elif value_type == 'dateTime.iso8601':
value = datetime.strptime(value, '%Y%m%dT%H:%M:%S')
else:
# Default is 'string'
value = str(value)

# Device not found
hmdevice = _device_from_servicecall(hass, service)
Expand Down

0 comments on commit ca2a682

Please sign in to comment.