Skip to content

Commit

Permalink
Change detection logic to better accomodate unit id 0
Browse files Browse the repository at this point in the history
  • Loading branch information
sfstar committed May 18, 2024
1 parent 8ebdcb5 commit db245cd
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 58 deletions.
17 changes: 9 additions & 8 deletions custom_components/victron/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ async def async_setup_entry(
_LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")

#VE.CAN device zero is present under unit 100. This seperates non system / settings entities into the seperate can device
if slave == 100 and not register_name.startswith(("settings", "system")) :
actual_id = 0
else:
actual_id = slave
# if slave == 100 and not register_name.startswith(("settings", "system")) :
# actual_id = 0
# else:
# actual_id = slave

if isinstance(registerInfo.entityType, BoolReadEntityType):
descriptions.append(VictronEntityDescription(
description = VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
slave=actual_id,
))
_LOGGER.debug("composed description == " + str(descriptions))
slave=slave,
)
_LOGGER.debug("composed description == " + str(description))
descriptions.append(description)

entities = []
entity = {}
Expand Down
17 changes: 9 additions & 8 deletions custom_components/victron/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@ async def async_setup_entry(
if not config_entry.options[CONF_ADVANCED_OPTIONS]:
continue

if slave == 100 and not register_name.startswith(("settings", "system")) :
actual_id = 0
else:
actual_id = slave
# if slave == 100 and not register_name.startswith(("settings", "system")) :
# actual_id = 0
# else:
# actual_id = slave

if isinstance(registerInfo.entityType, ButtonWriteType):
descriptions.append(VictronEntityDescription(
description = VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
slave=actual_id,
slave=slave,
device_class=ButtonDeviceClass.RESTART,
address=registerInfo.register,
))
_LOGGER.debug("composed description == " + str(descriptions))
)
_LOGGER.debug("composed description == " + str(description))
descriptions.append(description)

entities = []
entity = {}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/victron/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ class system_battery_state(Enum):
"system_bus_charge_power": RegisterInfo(866, INT16, UnitOfPower.WATT)
}

valid_unit_ids = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
valid_unit_ids = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 100, 101, 204, 205, 206, 207, 208, 209,
Expand Down
13 changes: 7 additions & 6 deletions custom_components/victron/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,25 @@ def determine_present_devices(self):
for key, register_definition in register_info_dict.items():
#VE.CAN device zero is present under unit 100. This seperates non system / settings entities into the seperate can device
if unit == 100 and not key.startswith(("settings", "system")) :
actual_id = 0
else:
actual_id = unit
continue
# actual_id = 0
# else:
# actual_id = unit

try:
address = self.get_first_register_id(register_definition)
count = self.calculate_register_count(register_definition)
result = self.read_holding_registers(actual_id, address, count)
result = self.read_holding_registers(unit, address, count)
if result.isError():
_LOGGER.debug("result is error for unit: " + str(actual_id) + " address: " + str(address) + " count: " + str(count))
_LOGGER.debug("result is error for unit: " + str(unit) + " address: " + str(address) + " count: " + str(count))
else:
working_registers.append(key)
except Exception as e: # pylint: disable=broad-except
_LOGGER.error(e)


if len(working_registers) > 0:
valid_devices[actual_id] = working_registers
valid_devices[unit] = working_registers
else:
_LOGGER.debug("no registers found for unit: " + str(unit))

Expand Down
15 changes: 8 additions & 7 deletions custom_components/victron/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ async def async_setup_entry(
for register_name, registerInfo in register_info_dict[name].items():
_LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")

if slave == 100 and not register_name.startswith(("settings", "system")) :
actual_id = 0
else:
actual_id = slave
# if slave == 100 and not register_name.startswith(("settings", "system")) :
# actual_id = 0
# else:
# actual_id = slave

if isinstance(registerInfo.entityType, SliderWriteType):
descriptions.append(VictronEntityDescription(
description = VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
slave=actual_id,
slave=slave,
native_unit_of_measurement=registerInfo.unit,
mode=NumberMode.SLIDER if config_entry.options[CONF_USE_SLIDERS] else NumberMode.BOX,
native_min_value=determine_min_value(registerInfo.unit, config_entry.options, registerInfo.entityType.powerType, registerInfo.entityType.negative),
Expand All @@ -81,8 +81,9 @@ async def async_setup_entry(
address=registerInfo.register,
scale = registerInfo.scale,
native_step = registerInfo.step
))
)
_LOGGER.debug("composed description == " + str(descriptions))
descriptions.append(description)

entities = []
entity = {}
Expand Down
20 changes: 11 additions & 9 deletions custom_components/victron/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,21 @@ async def async_setup_entry(
if isinstance(registerInfo.entityType, SelectWriteType):
_LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")

if slave == 100 and not register_name.startswith(("settings", "system")) :
actual_id = 0
else:
actual_id = slave

descriptions.append(VictronEntityDescription(
# if slave == 100 and not register_name.startswith(("settings", "system")) :
# actual_id = 0
# else:
# actual_id = slave
description = VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
slave=actual_id,
slave=slave,
options=registerInfo.entityType.options,
address=registerInfo.register,
))
_LOGGER.debug("composed description == " + str(descriptions))
)

descriptions.append(description)
_LOGGER.debug("composed description == " + str(description))

entities = []
entity = {}
Expand Down
23 changes: 12 additions & 11 deletions custom_components/victron/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,23 @@ async def async_setup_entry(
continue

#VE.CAN device zero is present under unit 100. This seperates non system / settings entities into the seperate can device
if slave == 100 and not register_name.startswith(("settings", "system")) :
actual_id = 0
else:
actual_id = slave


descriptions.append(VictronEntityDescription(
# if slave == 100 and not register_name.startswith(("settings", "system")) :
# actual_id = 0
# else:
# actual_id = slave

description = VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
native_unit_of_measurement=registerInfo.unit,
state_class=registerInfo.determine_stateclass(),
slave=actual_id,
slave=slave,
device_class=determine_victron_device_class(register_name, registerInfo.unit),
entity_type=registerInfo.entityType if isinstance(registerInfo.entityType, TextReadEntityType) else None
))
_LOGGER.debug("composed description == " + str(descriptions))
)
_LOGGER.debug("composed description == " + str(description))

descriptions.append(description)

entities = []
entity = {}
Expand Down Expand Up @@ -137,7 +138,7 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio
self.entity_type = description.entity_type

self._attr_unique_id = f"{description.slave}_{self.description.key}"
if description.slave not in (100, 225):
if description.slave not in (0, 100, 225):
self.entity_id = f"{SENSOR_DOMAIN}.{DOMAIN}_{self.description.key}_{description.slave}"
else:
self.entity_id = f"{SENSOR_DOMAIN}.{DOMAIN}_{self.description.key}"
Expand Down
17 changes: 9 additions & 8 deletions custom_components/victron/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,20 @@ async def async_setup_entry(
_LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")

#VE.CAN device zero is present under unit 100. This seperates non system / settings entities into the seperate can device
if slave == 100 and not register_name.startswith(("settings", "system")) :
actual_id = 0
else:
actual_id = slave
# if slave == 100 and not register_name.startswith(("settings", "system")) :
# actual_id = 0
# else:
# actual_id = slave

if isinstance(registerInfo.entityType, SwitchWriteType):
descriptions.append(VictronEntityDescription(
description = VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
slave=actual_id,
slave=slave,
address=registerInfo.register,
))
_LOGGER.debug("composed description == " + str(descriptions))
)
descriptions.append(description)
_LOGGER.debug("composed description == " + str(description))

entities = []
entity = {}
Expand Down

0 comments on commit db245cd

Please sign in to comment.