From 8ebdcb50ec30bca165c9011cd5d57eb82f719293 Mon Sep 17 00:00:00 2001 From: Sven ten Raa Date: Wed, 15 May 2024 10:55:39 +0200 Subject: [PATCH] move unit determination to the setup entry function --- custom_components/victron/binary_sensor.py | 24 +++++++++---------- custom_components/victron/button.py | 25 +++++++++----------- custom_components/victron/number.py | 26 ++++++++++----------- custom_components/victron/select.py | 27 +++++++++++----------- custom_components/victron/sensor.py | 23 +++++++++--------- custom_components/victron/switch.py | 27 +++++++++++----------- 6 files changed, 73 insertions(+), 79 deletions(-) diff --git a/custom_components/victron/binary_sensor.py b/custom_components/victron/binary_sensor.py index 56542f4..755880b 100644 --- a/custom_components/victron/binary_sensor.py +++ b/custom_components/victron/binary_sensor.py @@ -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 = {} @@ -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}" diff --git a/custom_components/victron/button.py b/custom_components/victron/button.py index e353bd9..c565c8e 100644 --- a/custom_components/victron/button.py +++ b/custom_components/victron/button.py @@ -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 = {} @@ -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}" diff --git a/custom_components/victron/number.py b/custom_components/victron/number.py index 28ca6cd..ec0924c 100644 --- a/custom_components/victron/number.py +++ b/custom_components/victron/number.py @@ -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), @@ -79,6 +82,7 @@ async def async_setup_entry( scale = registerInfo.scale, native_step = registerInfo.step )) + _LOGGER.debug("composed description == " + str(descriptions)) entities = [] entity = {} @@ -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}" diff --git a/custom_components/victron/select.py b/custom_components/victron/select.py index f23b912..0299974 100644 --- a/custom_components/victron/select.py +++ b/custom_components/victron/select.py @@ -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 = {} @@ -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}" diff --git a/custom_components/victron/sensor.py b/custom_components/victron/sensor.py index ecee526..4caa78f 100644 --- a/custom_components/victron/sensor.py +++ b/custom_components/victron/sensor.py @@ -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: @@ -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}" diff --git a/custom_components/victron/switch.py b/custom_components/victron/switch.py index 40981dc..267f3f2 100644 --- a/custom_components/victron/switch.py +++ b/custom_components/victron/switch.py @@ -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 = {} @@ -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}"