|
14 | 14 | ) |
15 | 15 | from homeassistant.util.dt import now as dt_now |
16 | 16 |
|
| 17 | +from .const import DOMAIN |
17 | 18 | from .shared.logging import setup_simple_telemetry |
18 | 19 |
|
19 | 20 | _LOGGER = logging.getLogger(__name__) |
@@ -1477,42 +1478,60 @@ def _extract_param_type(entity_id: str) -> str: |
1477 | 1478 | return "value" # Fallback |
1478 | 1479 |
|
1479 | 1480 | def find_entity(suffix: str) -> str | None: |
1480 | | - _LOGGER.info(f"[FIND ENTITY] Hledám entitu se suffixem: {suffix}") |
1481 | | - matching_entities = [] |
1482 | | - for entity in self.hass.states.async_all(): |
1483 | | - if entity.entity_id.endswith(suffix): |
1484 | | - matching_entities.append(entity.entity_id) |
| 1481 | + _LOGGER.info("[FIND ENTITY] Hledám cloud entitu se suffixem: %s", suffix) |
1485 | 1482 |
|
1486 | | - if matching_entities: |
1487 | | - # Preferuj klasické senzory před binary/ostatními |
1488 | | - preferred_prefixes = ("sensor.", "number.", "binary_sensor.") |
1489 | | - for prefix in preferred_prefixes: |
1490 | | - for entity_id in matching_entities: |
1491 | | - if entity_id.startswith(prefix): |
1492 | | - _LOGGER.info( |
1493 | | - "[FIND ENTITY] Vybrána preferovaná entita: %s", |
1494 | | - entity_id, |
1495 | | - ) |
1496 | | - return entity_id |
1497 | | - _LOGGER.info( |
1498 | | - f"[FIND ENTITY] Nalezeno {len(matching_entities)} entit: {matching_entities}" |
1499 | | - ) |
1500 | | - return matching_entities[0] |
1501 | | - else: |
1502 | | - _LOGGER.warning( |
1503 | | - f"[FIND ENTITY] NENALEZENA žádná entita se suffixem: {suffix}" |
1504 | | - ) |
1505 | | - # Debug: Vypíšeme všechny invertor entity |
1506 | | - all_invertor = [ |
1507 | | - e.entity_id |
1508 | | - for e in self.hass.states.async_all() |
1509 | | - if "invertor" in e.entity_id.lower() |
1510 | | - ] |
| 1483 | + # Preferuj pouze cloud senzory: sensor.oig_<box_id>_<suffix> |
| 1484 | + box_id = None |
| 1485 | + for key in ("box_id", "inverter_sn"): |
| 1486 | + val = self.entry.options.get(key) or self.entry.data.get(key) |
| 1487 | + if isinstance(val, str) and val.isdigit(): |
| 1488 | + box_id = val |
| 1489 | + break |
| 1490 | + |
| 1491 | + if not box_id: |
| 1492 | + try: |
| 1493 | + from .oig_cloud_sensor import resolve_box_id |
| 1494 | + |
| 1495 | + coordinator = None |
| 1496 | + for entry_data in self.hass.data.get(DOMAIN, {}).values(): |
| 1497 | + if not isinstance(entry_data, dict): |
| 1498 | + continue |
| 1499 | + if entry_data.get("service_shield") != self: |
| 1500 | + continue |
| 1501 | + coordinator = entry_data.get("coordinator") |
| 1502 | + break |
| 1503 | + if coordinator: |
| 1504 | + resolved = resolve_box_id(coordinator) |
| 1505 | + if isinstance(resolved, str) and resolved.isdigit(): |
| 1506 | + box_id = resolved |
| 1507 | + except Exception: |
| 1508 | + box_id = None |
| 1509 | + |
| 1510 | + if not box_id: |
1511 | 1511 | _LOGGER.warning( |
1512 | | - f"[FIND ENTITY] Všechny invertor entity: {all_invertor}" |
| 1512 | + "[FIND ENTITY] box_id nelze určit, cloud entitu pro suffix '%s' nelze vybrat", |
| 1513 | + suffix, |
1513 | 1514 | ) |
1514 | 1515 | return None |
1515 | 1516 |
|
| 1517 | + prefix = f"sensor.oig_{box_id}_" |
| 1518 | + matching_entities = [ |
| 1519 | + entity.entity_id |
| 1520 | + for entity in self.hass.states.async_all() |
| 1521 | + if entity.entity_id.startswith(prefix) |
| 1522 | + and entity.entity_id.endswith(suffix) |
| 1523 | + ] |
| 1524 | + |
| 1525 | + if matching_entities: |
| 1526 | + entity_id = matching_entities[0] |
| 1527 | + _LOGGER.info("[FIND ENTITY] Vybrána cloud entita: %s", entity_id) |
| 1528 | + return entity_id |
| 1529 | + |
| 1530 | + _LOGGER.warning( |
| 1531 | + "[FIND ENTITY] NENALEZENA cloud entita %s*%s", prefix, suffix |
| 1532 | + ) |
| 1533 | + return None |
| 1534 | + |
1516 | 1535 | # OPRAVA: Speciální handling pro set_formating_mode - nemá žádný senzor k ověření |
1517 | 1536 | if service_name == "oig_cloud.set_formating_mode": |
1518 | 1537 | # Vytvoříme fiktivní entitu pro 2minutové sledování |
|
0 commit comments