Merge pull request #2970 from persandstrom/modbus_write
modbus write register servicepull/3003/head
commit
04f0fec352
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue