2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Modbus switches."""
|
2015-04-15 14:47:42 +00:00
|
|
|
import logging
|
2020-02-12 17:37:16 +00:00
|
|
|
from typing import Optional
|
2019-02-24 09:22:17 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
from pymodbus.exceptions import ConnectionException, ModbusException
|
2020-02-12 17:37:16 +00:00
|
|
|
from pymodbus.pdu import ExceptionResponse
|
2016-09-13 20:47:44 +00:00
|
|
|
import voluptuous as vol
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2019-12-19 13:00:22 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA
|
2017-11-16 06:17:10 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_COMMAND_OFF,
|
|
|
|
CONF_COMMAND_ON,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_SLAVE,
|
|
|
|
STATE_ON,
|
|
|
|
)
|
2019-02-24 09:22:17 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2015-04-15 14:47:42 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2019-02-11 19:00:37 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2015-04-15 14:47:42 +00:00
|
|
|
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
from .const import (
|
|
|
|
CALL_TYPE_COIL,
|
|
|
|
CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
CALL_TYPE_REGISTER_INPUT,
|
|
|
|
CONF_COILS,
|
|
|
|
CONF_HUB,
|
|
|
|
CONF_REGISTER,
|
|
|
|
CONF_REGISTER_TYPE,
|
|
|
|
CONF_REGISTERS,
|
|
|
|
CONF_STATE_OFF,
|
|
|
|
CONF_STATE_ON,
|
|
|
|
CONF_VERIFY_REGISTER,
|
|
|
|
CONF_VERIFY_STATE,
|
|
|
|
DEFAULT_HUB,
|
|
|
|
MODBUS_DOMAIN,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2015-04-15 14:47:42 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-02-14 04:35:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
REGISTERS_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_COMMAND_OFF): cv.positive_int,
|
|
|
|
vol.Required(CONF_COMMAND_ON): cv.positive_int,
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_REGISTER): cv.positive_int,
|
|
|
|
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
vol.Optional(CONF_REGISTER_TYPE, default=CALL_TYPE_REGISTER_HOLDING): vol.In(
|
|
|
|
[CALL_TYPE_REGISTER_HOLDING, CALL_TYPE_REGISTER_INPUT]
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
vol.Optional(CONF_SLAVE): cv.positive_int,
|
|
|
|
vol.Optional(CONF_STATE_OFF): cv.positive_int,
|
|
|
|
vol.Optional(CONF_STATE_ON): cv.positive_int,
|
|
|
|
vol.Optional(CONF_VERIFY_REGISTER): cv.positive_int,
|
|
|
|
vol.Optional(CONF_VERIFY_STATE, default=True): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
COILS_SCHEMA = vol.Schema(
|
|
|
|
{
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
vol.Required(CALL_TYPE_COIL): cv.positive_int,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_SLAVE): cv.positive_int,
|
|
|
|
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-09-13 20:47:44 +00:00
|
|
|
|
2017-11-16 06:17:10 +00:00
|
|
|
PLATFORM_SCHEMA = vol.All(
|
|
|
|
cv.has_at_least_one_key(CONF_COILS, CONF_REGISTERS),
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_COILS): [COILS_SCHEMA],
|
|
|
|
vol.Optional(CONF_REGISTERS): [REGISTERS_SCHEMA],
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
2017-11-16 06:17:10 +00:00
|
|
|
|
2015-04-21 14:40:13 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Read configuration and create Modbus devices."""
|
2015-04-15 14:47:42 +00:00
|
|
|
switches = []
|
2017-11-16 06:17:10 +00:00
|
|
|
if CONF_COILS in config:
|
2020-02-19 03:41:12 +00:00
|
|
|
for coil in config[CONF_COILS]:
|
|
|
|
hub_name = coil[CONF_HUB]
|
2019-02-11 19:00:37 +00:00
|
|
|
hub = hass.data[MODBUS_DOMAIN][hub_name]
|
2019-07-31 19:25:30 +00:00
|
|
|
switches.append(
|
|
|
|
ModbusCoilSwitch(
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
hub, coil[CONF_NAME], coil[CONF_SLAVE], coil[CALL_TYPE_COIL]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2017-11-16 06:17:10 +00:00
|
|
|
if CONF_REGISTERS in config:
|
2020-02-19 03:41:12 +00:00
|
|
|
for register in config[CONF_REGISTERS]:
|
|
|
|
hub_name = register[CONF_HUB]
|
2019-02-11 19:00:37 +00:00
|
|
|
hub = hass.data[MODBUS_DOMAIN][hub_name]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
switches.append(
|
|
|
|
ModbusRegisterSwitch(
|
|
|
|
hub,
|
2020-02-19 03:41:12 +00:00
|
|
|
register[CONF_NAME],
|
2019-07-31 19:25:30 +00:00
|
|
|
register.get(CONF_SLAVE),
|
2020-02-19 03:41:12 +00:00
|
|
|
register[CONF_REGISTER],
|
|
|
|
register[CONF_COMMAND_ON],
|
|
|
|
register[CONF_COMMAND_OFF],
|
|
|
|
register[CONF_VERIFY_STATE],
|
2019-07-31 19:25:30 +00:00
|
|
|
register.get(CONF_VERIFY_REGISTER),
|
2020-02-19 03:41:12 +00:00
|
|
|
register[CONF_REGISTER_TYPE],
|
2019-07-31 19:25:30 +00:00
|
|
|
register.get(CONF_STATE_ON),
|
|
|
|
register.get(CONF_STATE_OFF),
|
|
|
|
)
|
|
|
|
)
|
2019-02-14 04:35:12 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
add_entities(switches)
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2015-04-21 14:40:13 +00:00
|
|
|
|
2019-02-11 19:00:37 +00:00
|
|
|
class ModbusCoilSwitch(ToggleEntity, RestoreEntity):
|
2017-11-16 06:17:10 +00:00
|
|
|
"""Representation of a Modbus coil switch."""
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2019-02-11 19:00:37 +00:00
|
|
|
def __init__(self, hub, name, slave, coil):
|
2017-11-16 06:17:10 +00:00
|
|
|
"""Initialize the coil switch."""
|
2019-02-11 19:00:37 +00:00
|
|
|
self._hub = hub
|
2015-04-15 14:47:42 +00:00
|
|
|
self._name = name
|
2016-09-13 20:47:44 +00:00
|
|
|
self._slave = int(slave) if slave else None
|
|
|
|
self._coil = int(coil)
|
2015-04-15 14:47:42 +00:00
|
|
|
self._is_on = None
|
2020-02-12 17:37:16 +00:00
|
|
|
self._available = True
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2019-02-11 19:00:37 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Handle entity which will be added."""
|
|
|
|
state = await self.async_get_last_state()
|
|
|
|
if not state:
|
|
|
|
return
|
|
|
|
self._is_on = state.state == STATE_ON
|
|
|
|
|
2015-04-15 14:47:42 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if switch is on."""
|
2015-04-15 14:47:42 +00:00
|
|
|
return self._is_on
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the name of the switch."""
|
2015-04-15 14:47:42 +00:00
|
|
|
return self._name
|
|
|
|
|
2020-02-12 17:37:16 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self._available
|
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Set switch on."""
|
2020-04-17 07:55:57 +00:00
|
|
|
self._write_coil(self._coil, True)
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Set switch off."""
|
2020-04-17 07:55:57 +00:00
|
|
|
self._write_coil(self._coil, False)
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def update(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Update the state of the switch."""
|
2020-04-17 07:55:57 +00:00
|
|
|
self._is_on = self._read_coil(self._coil)
|
2020-02-12 17:37:16 +00:00
|
|
|
|
2020-09-23 13:50:01 +00:00
|
|
|
def _read_coil(self, coil) -> bool:
|
2020-02-12 17:37:16 +00:00
|
|
|
"""Read coil using the Modbus hub slave."""
|
2020-04-17 07:55:57 +00:00
|
|
|
try:
|
|
|
|
result = self._hub.read_coils(self._slave, coil, 1)
|
|
|
|
except ConnectionException:
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
self._available = False
|
2020-09-23 13:50:01 +00:00
|
|
|
return False
|
2020-04-17 07:55:57 +00:00
|
|
|
|
2020-02-12 17:37:16 +00:00
|
|
|
if isinstance(result, (ModbusException, ExceptionResponse)):
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
self._available = False
|
2020-09-23 13:50:01 +00:00
|
|
|
return False
|
2020-02-12 17:37:16 +00:00
|
|
|
|
|
|
|
self._available = True
|
2020-09-23 13:50:01 +00:00
|
|
|
# bits[0] select the lowest bit in result,
|
|
|
|
# is_on for a binary_sensor is true if the bit are 1
|
|
|
|
# The other bits are not considered.
|
|
|
|
return bool(result.bits[0])
|
2020-02-12 17:37:16 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def _write_coil(self, coil, value):
|
2020-02-12 17:37:16 +00:00
|
|
|
"""Write coil using the Modbus hub slave."""
|
2020-04-17 07:55:57 +00:00
|
|
|
try:
|
|
|
|
self._hub.write_coil(self._slave, coil, value)
|
|
|
|
except ConnectionException:
|
|
|
|
self._available = False
|
|
|
|
return
|
|
|
|
|
2020-02-12 17:37:16 +00:00
|
|
|
self._available = True
|
|
|
|
|
2017-11-16 06:17:10 +00:00
|
|
|
|
|
|
|
class ModbusRegisterSwitch(ModbusCoilSwitch):
|
|
|
|
"""Representation of a Modbus register switch."""
|
|
|
|
|
|
|
|
# pylint: disable=super-init-not-called
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hub,
|
|
|
|
name,
|
|
|
|
slave,
|
|
|
|
register,
|
|
|
|
command_on,
|
|
|
|
command_off,
|
|
|
|
verify_state,
|
|
|
|
verify_register,
|
|
|
|
register_type,
|
|
|
|
state_on,
|
|
|
|
state_off,
|
|
|
|
):
|
2017-11-16 06:17:10 +00:00
|
|
|
"""Initialize the register switch."""
|
2019-02-11 19:00:37 +00:00
|
|
|
self._hub = hub
|
2017-11-16 06:17:10 +00:00
|
|
|
self._name = name
|
|
|
|
self._slave = slave
|
|
|
|
self._register = register
|
|
|
|
self._command_on = command_on
|
|
|
|
self._command_off = command_off
|
|
|
|
self._verify_state = verify_state
|
2019-07-31 19:25:30 +00:00
|
|
|
self._verify_register = verify_register if verify_register else self._register
|
2017-11-16 06:17:10 +00:00
|
|
|
self._register_type = register_type
|
2020-02-12 17:37:16 +00:00
|
|
|
self._available = True
|
2017-12-21 13:24:19 +00:00
|
|
|
|
|
|
|
if state_on is not None:
|
|
|
|
self._state_on = state_on
|
|
|
|
else:
|
|
|
|
self._state_on = self._command_on
|
|
|
|
|
|
|
|
if state_off is not None:
|
|
|
|
self._state_off = state_off
|
|
|
|
else:
|
|
|
|
self._state_off = self._command_off
|
|
|
|
|
2017-11-16 06:17:10 +00:00
|
|
|
self._is_on = None
|
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2017-11-16 06:17:10 +00:00
|
|
|
"""Set switch on."""
|
2020-02-12 17:37:16 +00:00
|
|
|
|
|
|
|
# Only holding register is writable
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
if self._register_type == CALL_TYPE_REGISTER_HOLDING:
|
2020-04-17 07:55:57 +00:00
|
|
|
self._write_register(self._command_on)
|
2020-02-12 17:37:16 +00:00
|
|
|
if not self._verify_state:
|
|
|
|
self._is_on = True
|
2017-11-16 06:17:10 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2017-11-16 06:17:10 +00:00
|
|
|
"""Set switch off."""
|
2020-02-12 17:37:16 +00:00
|
|
|
|
|
|
|
# Only holding register is writable
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
if self._register_type == CALL_TYPE_REGISTER_HOLDING:
|
2020-04-17 07:55:57 +00:00
|
|
|
self._write_register(self._command_off)
|
2020-02-12 17:37:16 +00:00
|
|
|
if not self._verify_state:
|
|
|
|
self._is_on = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self._available
|
2017-11-16 06:17:10 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def update(self):
|
2017-11-16 06:17:10 +00:00
|
|
|
"""Update the state of the switch."""
|
|
|
|
if not self._verify_state:
|
|
|
|
return
|
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
value = self._read_register()
|
2017-11-16 06:17:10 +00:00
|
|
|
if value == self._state_on:
|
|
|
|
self._is_on = True
|
|
|
|
elif value == self._state_off:
|
|
|
|
self._is_on = False
|
2020-02-12 17:37:16 +00:00
|
|
|
elif value is not None:
|
2017-11-16 06:17:10 +00:00
|
|
|
_LOGGER.error(
|
2020-01-02 19:17:10 +00:00
|
|
|
"Unexpected response from hub %s, slave %s register %s, got 0x%2x",
|
2019-07-31 19:25:30 +00:00
|
|
|
self._hub.name,
|
|
|
|
self._slave,
|
2020-02-12 17:37:16 +00:00
|
|
|
self._register,
|
2019-07-31 19:25:30 +00:00
|
|
|
value,
|
|
|
|
)
|
2020-02-12 17:37:16 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def _read_register(self) -> Optional[int]:
|
|
|
|
try:
|
|
|
|
if self._register_type == CALL_TYPE_REGISTER_INPUT:
|
2020-05-06 12:07:09 +00:00
|
|
|
result = self._hub.read_input_registers(
|
|
|
|
self._slave, self._verify_register, 1
|
|
|
|
)
|
2020-04-17 07:55:57 +00:00
|
|
|
else:
|
|
|
|
result = self._hub.read_holding_registers(
|
2020-05-06 12:07:09 +00:00
|
|
|
self._slave, self._verify_register, 1
|
2020-04-17 07:55:57 +00:00
|
|
|
)
|
|
|
|
except ConnectionException:
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
self._available = False
|
2020-02-12 17:37:16 +00:00
|
|
|
return
|
2020-04-17 07:55:57 +00:00
|
|
|
|
2020-02-12 17:37:16 +00:00
|
|
|
if isinstance(result, (ModbusException, ExceptionResponse)):
|
Modbus patch, to allow communication with "slow" equipment using tcp (#32557)
* modbus: bumb pymodbus version to 2.3.0
pymodbus version 1.5.2 did not support asyncio, and in general
the async handling have been improved a lot in version 2.3.0.
updated core/requirement*txt
* updated core/CODEOWNERS
committing result of 'python3 -m script.hassfest'.
* modbus: change core connection to async
change setup() --> async_setup and update() --> async_update()
Use async_setup_platform() to complete the async connection to core.
listen for EVENT_HOMEASSISTANT_START happens in async_setup()
so it needs to be async_listen.
But listen for EVENT_HOMEASSISTANT_STOP happens in start_modbus()
which is a sync. function so it continues to be listen().
* modbus: move setup of pymodbus into modbushub
setup of pymodbus is logically connected to the class modbushub,
therefore move it into the class.
Delay construction of pymodbus client until event
EVENT_HOMEASSISTANT_START arrives.
* modbus: use pymodbus async library
convert pymodbus calls to refer to the async library.
Remark: connect() is no longer needed, it is done when constructing
the client. There are also automatic reconnect.
* modbus: use async update for read/write
Use async functions for read/write from pymodbus.
change thread.Lock() to asyncio.Lock()
* Modbus: patch for slow tcp equipment
When connecting, via Modbus-TCP, so some equipment (like the
huawei sun2000 inverter), they need time to prepare the protocol.
Solution is to add a asyncio.sleep(x) after the connect() and before
sending the first message.
Add optional parameter "delay" to Modbus configuration.
Default is 0, which means do not execute asyncio.sleep().
* Modbus: silence pylint false positive
pylint does not accept that a class construction __new__
can return a tuple.
* Modbus: move constants to const.py
Create const.py with constants only used in
the modbus integration.
Duplicate entries are removed, but NOT any entry that would
lead to a configuration change.
Some entries were the same but with different names, in this
case renaming is done.
Also correct the tests.
* Modbus: move connection error handling to ModbusHub
Connection error handling depends on the hub, not the
entity, therefore it is logical to have the handling in
ModbusHub.
All pymodbus call are added to 2 generic functions (read/write)
in order not to duplicate the error handling code.
Added property "available" to signal if the hub is connected.
* Modbus: CI cleanup
Solve CI problems.
* Modbus: remove close of client
close() no longer exist in the pymodbus library, use
del client instead.
* Modbus: correct review comments
Adjust code based on review comments.
* Modbus: remove twister dependency
Pymodbus in asyncio mode do not use twister but still throws a
warning if twister is not installed, this warning goes into
homeassistant.log and can thus cause confusion among users.
However installing twister just to avoid the warning is not
the best solution, therefore removing dependency on twister.
* Modbus: review, remove comments.
remove commented out code.
2020-03-29 17:39:30 +00:00
|
|
|
self._available = False
|
2020-02-12 17:37:16 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self._available = True
|
|
|
|
|
2020-05-08 22:10:17 +00:00
|
|
|
return int(result.registers[0])
|
2020-02-12 17:37:16 +00:00
|
|
|
|
2020-04-17 07:55:57 +00:00
|
|
|
def _write_register(self, value):
|
2020-02-12 17:37:16 +00:00
|
|
|
"""Write holding register using the Modbus hub slave."""
|
2020-04-17 07:55:57 +00:00
|
|
|
try:
|
|
|
|
self._hub.write_register(self._slave, self._register, value)
|
|
|
|
except ConnectionException:
|
|
|
|
self._available = False
|
|
|
|
return
|
|
|
|
|
2020-02-12 17:37:16 +00:00
|
|
|
self._available = True
|