Skip to content

Commit 62e9b99

Browse files
committed
Fix shield to use cloud entities only
1 parent 44bbe4a commit 62e9b99

File tree

2 files changed

+62
-31
lines changed

2 files changed

+62
-31
lines changed

custom_components/oig_cloud/service_shield.py

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
from homeassistant.util.dt import now as dt_now
1616

17+
from .const import DOMAIN
1718
from .shared.logging import setup_simple_telemetry
1819

1920
_LOGGER = logging.getLogger(__name__)
@@ -1477,42 +1478,60 @@ def _extract_param_type(entity_id: str) -> str:
14771478
return "value" # Fallback
14781479

14791480
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)
14851482

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:
15111511
_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,
15131514
)
15141515
return None
15151516

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+
15161535
# OPRAVA: Speciální handling pro set_formating_mode - nemá žádný senzor k ověření
15171536
if service_name == "oig_cloud.set_formating_mode":
15181537
# Vytvoříme fiktivní entitu pro 2minutové sledování

custom_components/oig_cloud/strings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@
200200
"distribution_fee_nt_kwh": "Poplatek za distribuci NT (CZK/kWh)",
201201
"tariff_vt_start_weekday": "VT začátek pracovní den (hodina)",
202202
"tariff_nt_start_weekday": "NT začátek pracovní den (hodina1,hodina2)",
203+
"tariff_vt_start_weekend": "VT začátek víkend (hodina)",
204+
"tariff_nt_start_weekend": "NT začátek víkend (hodina1,hodina2)",
205+
"tariff_weekend_same_as_weekday": "Víkend stejné jako pracovní dny",
203206
"vat_rate": "DPH (%)",
204207
"go_back": "⬅️ Chci se vrátit zpět (zaškrtnout + kliknout Odeslat)"
205208
},
@@ -209,6 +212,9 @@
209212
"distribution_fee_nt_kwh": "Distribuční poplatek NT (nízký tarif). Např. 0.91 CZK/kWh",
210213
"tariff_vt_start_weekday": "Začátek VT v pracovní den. Formát: hodina (např. '6' = 06:00)",
211214
"tariff_nt_start_weekday": "Začátek NT v pracovní den. Formát: hodina1,hodina2 (např. '22,2' = 22:00 večer a 02:00 ráno)",
215+
"tariff_vt_start_weekend": "Začátek VT o víkendu. Formát: hodina (nechte prázdné pro NT celý den)",
216+
"tariff_nt_start_weekend": "Začátek NT o víkendu. Formát: hodina1,hodina2 (např. '0' = NT celý den)",
217+
"tariff_weekend_same_as_weekday": "Použít stejné časy VT/NT i o víkendu",
212218
"vat_rate": "Sazba DPH v procentech. Standardně 21%",
213219
"go_back": "Zaškrtněte toto políčko a stiskněte tlačítko 'Odeslat' - vrátíte se o krok zpět"
214220
}
@@ -455,6 +461,9 @@
455461
"distribution_fee_nt_kwh": "Poplatek za distribuci NT (CZK/kWh)",
456462
"tariff_vt_start_weekday": "VT začátek pracovní den (hodina)",
457463
"tariff_nt_start_weekday": "NT začátek pracovní den (hodina1,hodina2)",
464+
"tariff_vt_start_weekend": "VT začátek víkend (hodina)",
465+
"tariff_nt_start_weekend": "NT začátek víkend (hodina1,hodina2)",
466+
"tariff_weekend_same_as_weekday": "Víkend stejné jako pracovní dny",
458467
"vat_rate": "DPH (%)",
459468
"go_back": "⬅️ Chci se vrátit zpět (zaškrtnout + kliknout Odeslat)"
460469
},
@@ -464,6 +473,9 @@
464473
"distribution_fee_nt_kwh": "Distribuční poplatek NT (nízký tarif). Např. 0.91 CZK/kWh",
465474
"tariff_vt_start_weekday": "Začátek VT v pracovní den. Formát: hodina (např. '6' = 06:00)",
466475
"tariff_nt_start_weekday": "Začátek NT v pracovní den. Formát: hodina1,hodina2 (např. '22,2' = 22:00 večer a 02:00 ráno)",
476+
"tariff_vt_start_weekend": "Začátek VT o víkendu. Formát: hodina (nechte prázdné pro NT celý den)",
477+
"tariff_nt_start_weekend": "Začátek NT o víkendu. Formát: hodina1,hodina2 (např. '0' = NT celý den)",
478+
"tariff_weekend_same_as_weekday": "Použít stejné časy VT/NT i o víkendu",
467479
"vat_rate": "Sazba DPH v procentech. Standardně 21%",
468480
"go_back": "Zaškrtněte toto políčko a stiskněte tlačítko 'Odeslat' - vrátíte se o krok zpět"
469481
}

0 commit comments

Comments
 (0)