Skip to content

Commit 1077ecd

Browse files
author
Jan C
committed
fix proposed by @thomas7475 - is_entity_enabled detection
1 parent c87ac56 commit 1077ecd

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

custom_components/solax_modbus/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from homeassistant.helpers.device_registry import DeviceInfo
4242
from homeassistant.helpers import entity_registry as er
4343

44-
RETRIES = 0 # was 6
44+
RETRIES = 1 #was 6 then 0, which worked also, but 1 is probably the safe choice
4545
INVALID_START = 99999
4646

4747

@@ -263,15 +263,20 @@ def Gen4Timestring(numb):
263263

264264

265265
def is_entity_enabled(hass, hubname, descriptor): # Check if the entity is enabled in Home Assistant
266-
entity_id = f"sensor.{hubname}_{descriptor.key}"
267-
"""Check if an entity is enabled in the entity registry."""
266+
unique_id = f"{hubname}_{descriptor.key}"
267+
platform = "sensor"
268+
#Check if an entity is enabled in the entity registry
268269
registry = er.async_get(hass)
270+
entity_id = registry.async_get_entity_id(platform, DOMAIN, unique_id)
269271
entity_entry = registry.async_get(entity_id)
272+
270273
# If an entity is not in the registry, it is probably a new one.
271274
# return True #Apply the default specified in the descriptor
272275
if entity_entry is None:
273-
_LOGGER.debug(f"Entity {entity_id} not found in entity registry, applying default {descriptor.entity_registry_enabled_default}")
274-
return True #descriptor.entity_registry_enabled_default
276+
_LOGGER.debug(f"Entity {unique_id} not found in entity registry, "
277+
f"applying default {descriptor.entity_registry_enabled_default}"
278+
)
279+
return descriptor.entity_registry_enabled_default
275280

276281
# Otherwise, return the inverse of the 'disabled' attribute
277282
if entity_entry.disabled:
@@ -498,6 +503,7 @@ def device_group_key(self, device_info: DeviceInfo):
498503

499504
return key
500505

506+
# following function is the added_to_hass callback for sensors, numbers and selects
501507
@callback
502508
async def async_add_solax_modbus_sensor(self, sensor: SolaXModbusSensor):
503509
"""Listen for data updates."""
@@ -518,10 +524,11 @@ async def _refresh(_now: Optional[int] = None) -> None:
518524

519525
device_key = self.device_group_key(sensor.device_info)
520526
grp = interval_group.device_groups.setdefault(device_key, empty_hub_device_group_lambda())
521-
_LOGGER.debug(f"adding sensor {sensor.entity_description.key} ")
527+
_LOGGER.debug(f"*** debug *** adding sensor {sensor.entity_description.key} available: {sensor._attr_available} ")
522528
grp.sensors.append(sensor)
523529
self.blocks_changed = True # will force rebuild_blocks to be called
524530

531+
525532
@callback
526533
async def async_remove_solax_modbus_sensor(self, sensor):
527534
"""Remove data update."""

0 commit comments

Comments
 (0)