Skip to content

Commit fbd69e6

Browse files
authored
Merge pull request #892 from canton7/bugfix/setup-new-ha-2025.8
Fix error setting up integration from fresh on HA 2025.8
2 parents 7d152ae + 11de469 commit fbd69e6

File tree

7 files changed

+42
-15
lines changed

7 files changed

+42
-15
lines changed

custom_components/foxess_modbus/entities/entity_descriptions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,7 +2575,7 @@ def _configuration_entities() -> Iterable[EntityFactory]:
25752575
)
25762576

25772577

2578-
ENTITIES: list[EntityFactory] = list(
2578+
ENTITIES: list[EntityFactory] = sorted(
25792579
itertools.chain(
25802580
_version_entities(),
25812581
_pv_entities(),
@@ -2586,5 +2586,6 @@ def _configuration_entities() -> Iterable[EntityFactory]:
25862586
_configuration_entities(),
25872587
(description for x in CHARGE_PERIODS for description in x.entity_descriptions),
25882588
REMOTE_CONTROL_DESCRIPTION.entity_descriptions,
2589-
)
2589+
),
2590+
key=lambda x: x.depends_on_other_entities,
25902591
)

custom_components/foxess_modbus/entities/entity_factory.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class EntityFactory(ABC, metaclass=EntityFactoryMetaclass): # type: ignore
3636
def entity_type(self) -> type[Entity]:
3737
"""Fetch the type of entity that this factory creates"""
3838

39+
@property
40+
def depends_on_other_entities(self) -> bool:
41+
"""Return true if this entity depends on other entities, and so should be constructed last."""
42+
return False
43+
3944
@abstractmethod
4045
def create_entity_if_supported(
4146
self,

custom_components/foxess_modbus/entities/modbus_integration_sensor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class ModbusIntegrationSensorDescription(SensorEntityDescription, EntityFactory)
4444
def entity_type(self) -> type[Entity]:
4545
return SensorEntity
4646

47+
@property
48+
def depends_on_other_entities(self) -> bool:
49+
return True
50+
4751
def create_entity_if_supported(
4852
self,
4953
controller: EntityController,

custom_components/foxess_modbus/inverter_profiles.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,26 @@ def create_entities(
177177
self,
178178
entity_type: type[Entity],
179179
controller: EntityController,
180+
filter_depends_on_other_entites: bool | None = None,
180181
) -> list[Entity]:
181182
"""Create all of the entities of the given type which support this inverter/connection combination"""
182183

183184
result = []
184185

185186
for entity_factory in ENTITIES:
186-
if entity_factory.entity_type == entity_type:
187-
entity = entity_factory.create_entity_if_supported(
188-
controller, self._get_inv(controller), self.register_type
189-
)
190-
if entity is not None:
191-
result.append(entity)
187+
if (
188+
filter_depends_on_other_entites is not None
189+
and filter_depends_on_other_entites != entity_factory.depends_on_other_entities
190+
):
191+
continue
192+
if entity_factory.entity_type != entity_type:
193+
continue
194+
195+
entity = entity_factory.create_entity_if_supported(
196+
controller, self._get_inv(controller), self.register_type
197+
)
198+
if entity is not None:
199+
result.append(entity)
192200

193201
return result
194202

@@ -434,11 +442,13 @@ def inverter_capacity(self, inverter_model: str) -> int:
434442
assert all(ConnectionType.AUX in x.connection_types for x in _INVERTER_PROFILES_LIST)
435443

436444

437-
def create_entities(entity_type: type[Entity], controller: EntityController) -> list[Entity]:
445+
def create_entities(
446+
entity_type: type[Entity], controller: EntityController, filter_depends_on_other_entites: bool | None = None
447+
) -> list[Entity]:
438448
"""Create all of the entities which support the inverter described by the given configuration object"""
439449

440450
return inverter_connection_type_profile_from_config(controller.inverter_details).create_entities(
441-
entity_type, controller
451+
entity_type, controller, filter_depends_on_other_entites
442452
)
443453

444454

custom_components/foxess_modbus/sensor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_d
2323

2424
for controller in controllers:
2525
async_add_devices([ConnectionStatusSensor(controller)])
26-
async_add_devices(create_entities(SensorEntity, controller))
26+
# We have to add sensors which don't depend on other sensors, before we add the sensors which *do* depend on
27+
# other sensors (like the integration sensors), otherwise HA crashes when trying to create the IntegrationSensor
28+
# because it can't find the sensor it depends on. See https://github.com/nathanmarlor/foxess_modbus/issues/886
29+
async_add_devices(create_entities(SensorEntity, controller, filter_depends_on_other_entites=False))
30+
async_add_devices(create_entities(SensorEntity, controller, filter_depends_on_other_entites=True))

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
homeassistant==2025.1.1
1+
homeassistant==2025.8.0
22

33
# Testing
4-
pytest-homeassistant-custom-component==0.13.202 # Matching version for 2025.1.0
4+
pytest-homeassistant-custom-component==0.13.269 # Matching version
55
psutil-home-assistant # Not sure why this is needed?
66
fnv_hash_fast # Or this?
77
pytest-asyncio
@@ -16,6 +16,6 @@ ruff==0.9.4
1616
mypy==1.15.0
1717

1818
# Typing
19-
homeassistant-stubs==2025.1.1 # Matching HA version
19+
homeassistant-stubs==2025.8.0 # Matching HA version
2020
types-python-slugify==8.0.0.2
2121
voluptuous-stubs==0.1.1

tests/test_entity_descriptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ async def test_creates_all_entities(hass: HomeAssistant) -> None:
4141
ENTITY_ID_PREFIX: "",
4242
UNIQUE_ID_PREFIX: "",
4343
}
44+
4445
# Asserts if e.g. the ModbusAddressSpecs match
45-
create_entities(entity_type, controller)
46+
# We can't test IntegrationSensors (which have depends_on_other_entities=True), as HA throws up saying
47+
# that the entity it depends on doesn't exist (as we're not actually creating entities).
48+
create_entities(entity_type, controller, filter_depends_on_other_entites=False)
4649

4750

4851
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:

0 commit comments

Comments
 (0)