Merge pull request #2970 from persandstrom/modbus_write

modbus write register service
pull/3003/head
Robbie Trencheny 2016-08-27 03:07:34 -07:00 committed by GitHub
commit 04f0fec352
1 changed files with 16 additions and 0 deletions

View File

@ -31,6 +31,12 @@ IP_PORT = "port"
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SERVICE_WRITE_REGISTER = "write_register"
ATTR_ADDRESS = "address"
ATTR_UNIT = "unit"
ATTR_VALUE = "value"
NETWORK = None NETWORK = None
TYPE = None TYPE = None
@ -74,6 +80,16 @@ def setup(hass, config):
NETWORK.connect() NETWORK.connect()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_modbus) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_modbus)
# Register services for modbus
hass.services.register(DOMAIN, SERVICE_WRITE_REGISTER, write_register)
def write_register(service):
"""Write modbus register."""
unit = int(float(service.data.get(ATTR_UNIT)))
address = int(float(service.data.get(ATTR_ADDRESS)))
value = int(float(service.data.get(ATTR_VALUE)))
NETWORK.write_register(address, value, unit=unit)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_modbus) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_modbus)
return True return True