Add lazy_error_count to modbus (#54412)

* Add lazy_error_count.

* Use -=

* Review comments.
pull/54953/head
jan iversen 2021-08-21 15:49:50 +02:00 committed by GitHub
parent 51434c5faa
commit 33f660118f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 47 additions and 1 deletions

View File

@ -646,6 +646,7 @@ omit =
homeassistant/components/modbus/cover.py
homeassistant/components/modbus/climate.py
homeassistant/components/modbus/modbus.py
homeassistant/components/modbus/sensor.py
homeassistant/components/modbus/validators.py
homeassistant/components/modem_callerid/sensor.py
homeassistant/components/motion_blinds/__init__.py

View File

@ -65,6 +65,7 @@ from .const import (
CONF_DATA_TYPE,
CONF_FANS,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_MAX_TEMP,
CONF_MIN_TEMP,
CONF_MSG_WAIT,
@ -136,6 +137,7 @@ BASE_COMPONENT_SCHEMA = vol.Schema(
vol.Optional(
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
): cv.positive_int,
vol.Optional(CONF_LAZY_ERROR, default=0): cv.positive_int,
}
)

View File

@ -36,6 +36,7 @@ from .const import (
CALL_TYPE_X_REGISTER_HOLDINGS,
CONF_DATA_TYPE,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_PRECISION,
CONF_SCALE,
CONF_STATE_OFF,
@ -76,6 +77,8 @@ class BasePlatform(Entity):
self._attr_device_class = entry.get(CONF_DEVICE_CLASS)
self._attr_available = True
self._attr_unit_of_measurement = None
self._lazy_error_count = entry[CONF_LAZY_ERROR]
self._lazy_errors = self._lazy_error_count
@abstractmethod
async def async_update(self, now=None):
@ -245,10 +248,15 @@ class BaseSwitch(BasePlatform, ToggleEntity, RestoreEntity):
)
self._call_active = False
if result is None:
if self._lazy_errors:
self._lazy_errors -= 1
return
self._lazy_errors = self._lazy_error_count
self._attr_available = False
self.async_write_ha_state()
return
self._lazy_errors = self._lazy_error_count
self._attr_available = True
if self._verify_type == CALL_TYPE_COIL:
self._attr_is_on = bool(result.bits[0] & 1)

View File

@ -57,10 +57,15 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
)
self._call_active = False
if result is None:
if self._lazy_errors:
self._lazy_errors -= 1
return
self._lazy_errors = self._lazy_error_count
self._attr_available = False
self.async_write_ha_state()
return
self._lazy_errors = self._lazy_error_count
self._attr_is_on = result.bits[0] & 1
self._attr_available = True
self.async_write_ha_state()

View File

@ -162,9 +162,14 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
self._slave, register, self._count, register_type
)
if result is None:
if self._lazy_errors:
self._lazy_errors -= 1
return -1
self._lazy_errors = self._lazy_error_count
self._attr_available = False
return -1
self._lazy_errors = self._lazy_error_count
self._value = self.unpack_structure_result(result.registers)
self._attr_available = True

View File

@ -28,6 +28,7 @@ CONF_FANS = "fans"
CONF_HUB = "hub"
CONF_INPUTS = "inputs"
CONF_INPUT_TYPE = "input_type"
CONF_LAZY_ERROR = "lazy_error_count"
CONF_MAX_TEMP = "max_temp"
CONF_MIN_TEMP = "min_temp"
CONF_MSG_WAIT = "message_wait_milliseconds"

View File

@ -146,9 +146,14 @@ class ModbusCover(BasePlatform, CoverEntity, RestoreEntity):
)
self._call_active = False
if result is None:
if self._lazy_errors:
self._lazy_errors -= 1
return
self._lazy_errors = self._lazy_error_count
self._attr_available = False
self.async_write_ha_state()
return None
return
self._lazy_errors = self._lazy_error_count
self._attr_available = True
if self._input_type == CALL_TYPE_COIL:
self._set_attr_state(bool(result.bits[0] & 1))

View File

@ -64,10 +64,15 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
self._slave, self._address, self._count, self._input_type
)
if result is None:
if self._lazy_errors:
self._lazy_errors -= 1
return
self._lazy_errors = self._lazy_error_count
self._attr_available = False
self.async_write_ha_state()
return
self._attr_native_value = self.unpack_structure_result(result.registers)
self._lazy_errors = self._lazy_error_count
self._attr_available = True
self.async_write_ha_state()

View File

@ -6,6 +6,7 @@ from homeassistant.components.modbus.const import (
CALL_TYPE_COIL,
CALL_TYPE_DISCRETE,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
)
from homeassistant.const import (
CONF_ADDRESS,
@ -44,6 +45,7 @@ ENTITY_ID = f"{SENSOR_DOMAIN}.{TEST_ENTITY_NAME}"
CONF_SLAVE: 10,
CONF_INPUT_TYPE: CALL_TYPE_DISCRETE,
CONF_DEVICE_CLASS: "door",
CONF_LAZY_ERROR: 10,
}
]
},

View File

@ -6,6 +6,7 @@ from homeassistant.components.climate.const import HVAC_MODE_AUTO
from homeassistant.components.modbus.const import (
CONF_CLIMATES,
CONF_DATA_TYPE,
CONF_LAZY_ERROR,
CONF_TARGET_TEMP,
DATA_TYPE_FLOAT32,
DATA_TYPE_FLOAT64,
@ -49,6 +50,7 @@ ENTITY_ID = f"{CLIMATE_DOMAIN}.{TEST_ENTITY_NAME}"
CONF_SLAVE: 10,
CONF_SCAN_INTERVAL: 20,
CONF_COUNT: 2,
CONF_LAZY_ERROR: 10,
}
],
},

View File

@ -8,6 +8,7 @@ from homeassistant.components.modbus.const import (
CALL_TYPE_COIL,
CALL_TYPE_REGISTER_HOLDING,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_STATE_CLOSED,
CONF_STATE_CLOSING,
CONF_STATE_OPEN,
@ -54,6 +55,7 @@ ENTITY_ID = f"{COVER_DOMAIN}.{TEST_ENTITY_NAME}"
CONF_INPUT_TYPE: CALL_TYPE_REGISTER_HOLDING,
CONF_SLAVE: 10,
CONF_SCAN_INTERVAL: 20,
CONF_LAZY_ERROR: 10,
}
]
},

View File

@ -10,6 +10,7 @@ from homeassistant.components.modbus.const import (
CALL_TYPE_REGISTER_INPUT,
CONF_FANS,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_STATE_OFF,
CONF_STATE_ON,
CONF_VERIFY,
@ -73,6 +74,7 @@ ENTITY_ID = f"{FAN_DOMAIN}.{TEST_ENTITY_NAME}"
CONF_SLAVE: 1,
CONF_COMMAND_OFF: 0x00,
CONF_COMMAND_ON: 0x01,
CONF_LAZY_ERROR: 10,
CONF_VERIFY: {
CONF_INPUT_TYPE: CALL_TYPE_REGISTER_HOLDING,
CONF_ADDRESS: 1235,

View File

@ -9,6 +9,7 @@ from homeassistant.components.modbus.const import (
CALL_TYPE_REGISTER_HOLDING,
CALL_TYPE_REGISTER_INPUT,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_STATE_OFF,
CONF_STATE_ON,
CONF_VERIFY,
@ -62,6 +63,7 @@ ENTITY_ID = f"{LIGHT_DOMAIN}.{TEST_ENTITY_NAME}"
CONF_NAME: TEST_ENTITY_NAME,
CONF_ADDRESS: 1234,
CONF_WRITE_TYPE: CALL_TYPE_COIL,
CONF_LAZY_ERROR: 10,
}
]
},

View File

@ -6,6 +6,7 @@ from homeassistant.components.modbus.const import (
CALL_TYPE_REGISTER_INPUT,
CONF_DATA_TYPE,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_PRECISION,
CONF_REGISTERS,
CONF_SCALE,
@ -62,6 +63,7 @@ ENTITY_ID = f"{SENSOR_DOMAIN}.{TEST_ENTITY_NAME}"
CONF_PRECISION: 0,
CONF_SCALE: 1,
CONF_OFFSET: 0,
CONF_LAZY_ERROR: 10,
CONF_INPUT_TYPE: CALL_TYPE_REGISTER_HOLDING,
CONF_DEVICE_CLASS: "battery",
}

View File

@ -11,6 +11,7 @@ from homeassistant.components.modbus.const import (
CALL_TYPE_REGISTER_HOLDING,
CALL_TYPE_REGISTER_INPUT,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_STATE_OFF,
CONF_STATE_ON,
CONF_VERIFY,
@ -70,6 +71,7 @@ ENTITY_ID = f"{SWITCH_DOMAIN}.{TEST_ENTITY_NAME}"
CONF_NAME: TEST_ENTITY_NAME,
CONF_ADDRESS: 1234,
CONF_WRITE_TYPE: CALL_TYPE_COIL,
CONF_LAZY_ERROR: 10,
}
]
},