Skip to content

Commit

Permalink
move unit determination to the setup entry function
Browse files Browse the repository at this point in the history
  • Loading branch information
sfstar committed May 15, 2024
1 parent b925c45 commit 8ebdcb5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 79 deletions.
24 changes: 12 additions & 12 deletions custom_components/victron/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ async def async_setup_entry(
for slave, registerLedger in register_set.items():
for name in registerLedger:
for register_name, registerInfo in register_info_dict[name].items():
# _LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")
# _LOGGER.debug(str(registerInfo.slave))
_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 isinstance(registerInfo.entityType, BoolReadEntityType):
descriptions.append(VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
slave=slave,
slave=actual_id,
))
_LOGGER.debug("composed description == " + str(descriptions))

entities = []
entity = {}
Expand Down Expand Up @@ -82,15 +88,9 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio
self._attr_device_class = description.device_class
self._attr_name = f"{description.name}"

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

self._attr_unique_id = f"{actual_id}_{self.description.key}"
if actual_id not in (100, 225):
self.entity_id = f"{BINARY_SENSOR_DOMAIN}.{DOMAIN}_{self.description.key}_{actual_id}"
self._attr_unique_id = f"{self.description.slave}_{self.description.key}"
if self.description.slave not in (100, 225):
self.entity_id = f"{BINARY_SENSOR_DOMAIN}.{DOMAIN}_{self.description.key}_{self.description.slave}"
else:
self.entity_id = f"{BINARY_SENSOR_DOMAIN}.{DOMAIN}_{self.description.key}"

Expand Down
25 changes: 11 additions & 14 deletions custom_components/victron/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,30 @@ async def async_setup_entry(
"""Set up Victron energy binary sensor entries."""
_LOGGER.debug("attempting to setup button entities")
victron_coordinator: victronEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
_LOGGER.debug(victron_coordinator.processed_data()["register_set"])
_LOGGER.debug(victron_coordinator.processed_data()["data"])
descriptions = []
#TODO cleanup
register_set = victron_coordinator.processed_data()["register_set"]
for slave, registerLedger in register_set.items():
for name in registerLedger:
for register_name, registerInfo in register_info_dict[name].items():
# _LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")
# _LOGGER.debug(str(registerInfo.slave))
_LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")
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 isinstance(registerInfo.entityType, ButtonWriteType):
descriptions.append(VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
slave=slave,
slave=actual_id,
device_class=ButtonDeviceClass.RESTART,
address=registerInfo.register,
))
_LOGGER.debug("composed description == " + str(descriptions))

entities = []
entity = {}
Expand Down Expand Up @@ -79,15 +82,9 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio
self._attr_device_class = description.device_class
self._attr_name = f"{description.name}"

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

self._attr_unique_id = f"{actual_id}_{self.description.key}"
if actual_id not in (100, 225):
self.entity_id = f"{BUTTON_DOMAIN}.{DOMAIN}_{self.description.key}_{actual_id}"
self._attr_unique_id = f"{self.description.slave}_{self.description.key}"
if self.description.slave not in (100, 225):
self.entity_id = f"{BUTTON_DOMAIN}.{DOMAIN}_{self.description.key}_{self.description.slave}"
else:
self.entity_id = f"{BUTTON_DOMAIN}.{DOMAIN}_{self.description.key}"

Expand Down
26 changes: 12 additions & 14 deletions custom_components/victron/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ async def async_setup_entry(
for slave, registerLedger in register_set.items():
for name in registerLedger:
for register_name, registerInfo in register_info_dict[name].items():
# _LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")
# _LOGGER.debug(str(registerInfo.slave))
# _LOGGER.debug("register_name")
# _LOGGER.debug(register_name)
_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 isinstance(registerInfo.entityType, SliderWriteType):
descriptions.append(VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
slave=slave,
slave=actual_id,
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 @@ -79,6 +82,7 @@ async def async_setup_entry(
scale = registerInfo.scale,
native_step = registerInfo.step
))
_LOGGER.debug("composed description == " + str(descriptions))

entities = []
entity = {}
Expand Down Expand Up @@ -169,17 +173,11 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio

self.data_key = str(self.description.slave) + "." + str(self.description.key)

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

self._attr_native_value = self.description.value_fn(self.coordinator.processed_data(), self.description.slave, self.description.key)

self._attr_unique_id = f"{actual_id}_{self.description.key}"
if actual_id not in (100, 225):
self.entity_id = f"{NUMBER_DOMAIN}.{DOMAIN}_{self.description.key}_{actual_id}"
self._attr_unique_id = f"{self.description.slave}_{self.description.key}"
if self.description.slave not in (100, 225):
self.entity_id = f"{NUMBER_DOMAIN}.{DOMAIN}_{self.description.key}_{self.description.slave}"
else:
self.entity_id = f"{NUMBER_DOMAIN}.{DOMAIN}_{self.description.key}"

Expand Down
27 changes: 13 additions & 14 deletions custom_components/victron/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,25 @@ async def async_setup_entry(
#TODO cleanup
if config_entry.options[CONF_ADVANCED_OPTIONS]:
register_set = victron_coordinator.processed_data()["register_set"]
for unit, registerLedger in register_set.items():
for slave, registerLedger in register_set.items():
for name in registerLedger:
for register_name, registerInfo in register_info_dict[name].items():
if isinstance(registerInfo.entityType, SelectWriteType):
# _LOGGER.debug("unit == " + str(unit) + " registerLedger == " + str(registerLedger) + " registerInfo ")
# _LOGGER.debug("register_name")
# _LOGGER.debug(register_name)
_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(
key=register_name,
name=register_name.replace('_', ' '),
slave=unit,
slave=actual_id,
options=registerInfo.entityType.options,
address=registerInfo.register,
))
_LOGGER.debug("composed description == " + str(descriptions))

entities = []
entity = {}
Expand Down Expand Up @@ -83,15 +88,9 @@ def __init__(self, hass: HomeAssistant, coordinator: victronEnergyDeviceUpdateCo
#this needs to be changed to allow multiple of the same type
self._attr_name = f"{description.name}"

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

self._attr_unique_id = f"{actual_id}_{self.description.key}"
if actual_id not in (100, 225):
self.entity_id = f"{SELECT_DOMAIN}.{DOMAIN}_{self.description.key}_{actual_id}"
self._attr_unique_id = f"{self.description.slave}_{self.description.key}"
if self.description.slave not in (100, 225):
self.entity_id = f"{SELECT_DOMAIN}.{DOMAIN}_{self.description.key}_{self.description.slave}"
else:
self.entity_id = f"{SELECT_DOMAIN}.{DOMAIN}_{self.description.key}"

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 @@ -57,17 +57,24 @@ async def async_setup_entry(
if not isinstance(registerInfo.entityType, ReadEntityType) or isinstance(registerInfo.entityType, BoolReadEntityType):
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(
key=register_name,
name=register_name.replace('_', ' '),
native_unit_of_measurement=registerInfo.unit,
state_class=registerInfo.determine_stateclass(),
slave=slave,
slave=actual_id,
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))

entities = []
entity = {}
for description in descriptions:
Expand Down Expand Up @@ -129,15 +136,9 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio
self._attr_state_class = description.state_class
self.entity_type = description.entity_type

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

self._attr_unique_id = f"{actual_id}_{self.description.key}"
if actual_id not in (100, 225):
self.entity_id = f"{SENSOR_DOMAIN}.{DOMAIN}_{self.description.key}_{actual_id}"
self._attr_unique_id = f"{description.slave}_{self.description.key}"
if description.slave not in (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
27 changes: 13 additions & 14 deletions custom_components/victron/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,22 @@ async def async_setup_entry(
for slave, registerLedger in register_set.items():
for name in registerLedger:
for register_name, registerInfo in register_info_dict[name].items():
# _LOGGER.debug("unit == " + str(unit) + " registerLedger == " + str(registerLedger) + " registerInfo ")
# _LOGGER.debug(str(registerInfo.unit))
# _LOGGER.debug("register_name")
# _LOGGER.debug(register_name)
_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 isinstance(registerInfo.entityType, SwitchWriteType):
descriptions.append(VictronEntityDescription(
key=register_name,
name=register_name.replace('_', ' '),
slave=slave,
slave=actual_id,
address=registerInfo.register,
))
_LOGGER.debug("composed description == " + str(descriptions))

entities = []
entity = {}
Expand Down Expand Up @@ -77,15 +82,9 @@ def __init__(self, hass: HomeAssistant, coordinator: victronEnergyDeviceUpdateCo
self._attr_name = f"{description.name}"
self.data_key = str(self.description.slave) + "." + str(self.description.key)

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

self._attr_unique_id = f"{actual_id}_{self.description.key}"
if actual_id not in (100, 225):
self.entity_id = f"{SWITCH_DOMAIN}.{DOMAIN}_{self.description.key}_{actual_id}"
self._attr_unique_id = f"{description.slave}_{self.description.key}"
if description.slave not in (100, 225):
self.entity_id = f"{SWITCH_DOMAIN}.{DOMAIN}_{self.description.key}_{description.slave}"
else:
self.entity_id = f"{SWITCH_DOMAIN}.{DOMAIN}_{self.description.key}"

Expand Down

0 comments on commit 8ebdcb5

Please sign in to comment.