Convert last properties in modbus to _attr_variable (#53919)

This commit is contained in:
jan iversen 2021-08-08 23:23:21 +02:00 committed by GitHub
parent e8aa280d7f
commit 02459e6813
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 74 deletions

View File

@ -21,7 +21,7 @@ from homeassistant.const import (
CONF_STRUCTURE, CONF_STRUCTURE,
STATE_ON, STATE_ON,
) )
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity, ToggleEntity
from homeassistant.helpers.event import async_call_later, async_track_time_interval from homeassistant.helpers.event import async_call_later, async_track_time_interval
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
@ -124,10 +124,9 @@ class BaseStructPlatform(BasePlatform, RestoreEntity):
registers = self._swap_registers(registers) registers = self._swap_registers(registers)
byte_string = b"".join([x.to_bytes(2, byteorder="big") for x in registers]) byte_string = b"".join([x.to_bytes(2, byteorder="big") for x in registers])
if self._data_type == DATA_TYPE_STRING: if self._data_type == DATA_TYPE_STRING:
self._value = byte_string.decode() return byte_string.decode()
else:
val = struct.unpack(self._structure, byte_string)
val = struct.unpack(self._structure, byte_string)
# Issue: https://github.com/home-assistant/core/issues/41944 # Issue: https://github.com/home-assistant/core/issues/41944
# If unpack() returns a tuple greater than 1, don't try to process the value. # If unpack() returns a tuple greater than 1, don't try to process the value.
# Instead, return the values of unpack(...) separated by commas. # Instead, return the values of unpack(...) separated by commas.
@ -144,8 +143,8 @@ class BaseStructPlatform(BasePlatform, RestoreEntity):
v_result.append(str(v_temp)) v_result.append(str(v_temp))
else: else:
v_result.append(f"{float(v_temp):.{self._precision}f}") v_result.append(f"{float(v_temp):.{self._precision}f}")
self._value = ",".join(map(str, v_result)) return ",".join(map(str, v_result))
else:
# Apply scale and precision to floats and ints # Apply scale and precision to floats and ints
val = self._scale * val[0] + self._offset val = self._scale * val[0] + self._offset
@ -153,19 +152,18 @@ class BaseStructPlatform(BasePlatform, RestoreEntity):
# we lose some precision, and unit tests will fail. Therefore, we do # we lose some precision, and unit tests will fail. Therefore, we do
# the conversion only when it's absolutely necessary. # the conversion only when it's absolutely necessary.
if isinstance(val, int) and self._precision == 0: if isinstance(val, int) and self._precision == 0:
self._value = str(val) return str(val)
else: return f"{float(val):.{self._precision}f}"
self._value = f"{float(val):.{self._precision}f}"
class BaseSwitch(BasePlatform, RestoreEntity): class BaseSwitch(BasePlatform, ToggleEntity, RestoreEntity):
"""Base class representing a Modbus switch.""" """Base class representing a Modbus switch."""
def __init__(self, hub: ModbusHub, config: dict) -> None: def __init__(self, hub: ModbusHub, config: dict) -> None:
"""Initialize the switch.""" """Initialize the switch."""
config[CONF_INPUT_TYPE] = "" config[CONF_INPUT_TYPE] = ""
super().__init__(hub, config) super().__init__(hub, config)
self._is_on = None self._attr_is_on = False
convert = { convert = {
CALL_TYPE_REGISTER_HOLDING: ( CALL_TYPE_REGISTER_HOLDING: (
CALL_TYPE_REGISTER_HOLDING, CALL_TYPE_REGISTER_HOLDING,
@ -202,12 +200,7 @@ class BaseSwitch(BasePlatform, RestoreEntity):
await self.async_base_added_to_hass() await self.async_base_added_to_hass()
state = await self.async_get_last_state() state = await self.async_get_last_state()
if state: if state:
self._is_on = state.state == STATE_ON self._attr_is_on = state.state == STATE_ON
@property
def is_on(self):
"""Return true if switch is on."""
return self._is_on
async def async_turn(self, command): async def async_turn(self, command):
"""Evaluate switch result.""" """Evaluate switch result."""
@ -221,7 +214,7 @@ class BaseSwitch(BasePlatform, RestoreEntity):
self._attr_available = True self._attr_available = True
if not self._verify_active: if not self._verify_active:
self._is_on = command == self.command_on self._attr_is_on = command == self.command_on
self.async_write_ha_state() self.async_write_ha_state()
return return
@ -258,13 +251,13 @@ class BaseSwitch(BasePlatform, RestoreEntity):
self._attr_available = True self._attr_available = True
if self._verify_type == CALL_TYPE_COIL: if self._verify_type == CALL_TYPE_COIL:
self._is_on = bool(result.bits[0] & 1) self._attr_is_on = bool(result.bits[0] & 1)
else: else:
value = int(result.registers[0]) value = int(result.registers[0])
if value == self._state_on: if value == self._state_on:
self._is_on = True self._attr_is_on = True
elif value == self._state_off: elif value == self._state_off:
self._is_on = False self._attr_is_on = False
elif value is not None: elif value is not None:
_LOGGER.error( _LOGGER.error(
"Unexpected response from modbus device slave %s register %s, got 0x%2x", "Unexpected response from modbus device slave %s register %s, got 0x%2x",

View File

@ -43,14 +43,7 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
await self.async_base_added_to_hass() await self.async_base_added_to_hass()
state = await self.async_get_last_state() state = await self.async_get_last_state()
if state: if state:
self._value = state.state == STATE_ON self._attr_is_on = state.state == STATE_ON
else:
self._value = None
@property
def is_on(self):
"""Return the state of the sensor."""
return self._value
async def async_update(self, now=None): async def async_update(self, now=None):
"""Update the state of the sensor.""" """Update the state of the sensor."""
@ -68,6 +61,6 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
self.async_write_ha_state() self.async_write_ha_state()
return return
self._value = result.bits[0] & 1 self._attr_is_on = result.bits[0] & 1
self._attr_available = True self._attr_available = True
self.async_write_ha_state() self.async_write_ha_state()

View File

@ -165,8 +165,7 @@ class ModbusThermostat(BaseStructPlatform, RestoreEntity, ClimateEntity):
self._attr_available = False self._attr_available = False
return -1 return -1
self.unpack_structure_result(result.registers) self._value = self.unpack_structure_result(result.registers)
self._attr_available = True self._attr_available = True
if self._value is None: if self._value is None:

View File

@ -109,22 +109,13 @@ class ModbusCover(BasePlatform, CoverEntity, RestoreEntity):
STATE_UNAVAILABLE: None, STATE_UNAVAILABLE: None,
STATE_UNKNOWN: None, STATE_UNKNOWN: None,
} }
self._value = convert[state.state] self._set_attr_state(convert[state.state])
@property def _set_attr_state(self, value):
def is_opening(self): """Convert received value to HA state."""
"""Return if the cover is opening or not.""" self._attr_is_opening = value == self._state_opening
return self._value == self._state_opening self._attr_is_closing = value == self._state_closing
self._attr_is_closed = value == self._state_closed
@property
def is_closing(self):
"""Return if the cover is closing or not."""
return self._value == self._state_closing
@property
def is_closed(self):
"""Return if the cover is closed or not."""
return self._value == self._state_closed
async def async_open_cover(self, **kwargs: Any) -> None: async def async_open_cover(self, **kwargs: Any) -> None:
"""Open cover.""" """Open cover."""
@ -160,7 +151,7 @@ class ModbusCover(BasePlatform, CoverEntity, RestoreEntity):
return None return None
self._attr_available = True self._attr_available = True
if self._input_type == CALL_TYPE_COIL: if self._input_type == CALL_TYPE_COIL:
self._value = bool(result.bits[0] & 1) self._set_attr_state(bool(result.bits[0] & 1))
else: else:
self._value = int(result.registers[0]) self._set_attr_state(int(result.registers[0]))
self.async_write_ha_state() self.async_write_ha_state()

View File

@ -43,3 +43,11 @@ class ModbusFan(BaseSwitch, FanEntity):
) -> None: ) -> None:
"""Set fan on.""" """Set fan on."""
await self.async_turn(self.command_on) await self.async_turn(self.command_on)
@property
def is_on(self):
"""Return true if fan is on.
This is needed due to the ongoing conversion of fan.
"""
return self._attr_is_on

View File

@ -54,12 +54,7 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
await self.async_base_added_to_hass() await self.async_base_added_to_hass()
state = await self.async_get_last_state() state = await self.async_get_last_state()
if state: if state:
self._value = state.state self._attr_state = state.state
@property
def state(self):
"""Return the state of the sensor."""
return self._value
async def async_update(self, now=None): async def async_update(self, now=None):
"""Update the state of the sensor.""" """Update the state of the sensor."""
@ -73,6 +68,6 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
self.async_write_ha_state() self.async_write_ha_state()
return return
self.unpack_structure_result(result.registers) self._attr_state = self.unpack_structure_result(result.registers)
self._attr_available = True self._attr_available = True
self.async_write_ha_state() self.async_write_ha_state()