Skip to content

Commit d7a136c

Browse files
Merge pull request #68 from vincentwolsink/relay-updates
Update name of relay entity and add some entities
2 parents 830434b + 23e064c commit d7a136c

File tree

3 files changed

+88
-15
lines changed

3 files changed

+88
-15
lines changed

custom_components/enphase_envoy/binary_sensor.py

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ async def async_setup_entry(
5050
elif sensor_description.key == "relays":
5151
if coordinator.data.get("relays") is not None:
5252
for relay in coordinator.data["relays"]:
53-
device_name = f"{sensor_description.name} {relay}"
54-
entity_name = f"{name} {device_name}"
53+
device_name = f"Relay {relay}"
54+
entity_name = f"{device_name} {sensor_description.name}"
5555

5656
serial_number = relay
5757
entities.append(
58-
EnvoyRelayEntity(
58+
EnvoyRelayContactEntity(
5959
sensor_description,
6060
entity_name,
6161
device_name,
@@ -66,6 +66,28 @@ async def async_setup_entry(
6666
)
6767
)
6868

69+
elif sensor_description.key.startswith("relays_"):
70+
sensor_key = sensor_description.key.split("_", 1)[-1]
71+
if coordinator.data.get("relays") != None:
72+
for serial_number, data in coordinator.data["relays"].items():
73+
if data.get(sensor_key, None) == None:
74+
continue
75+
76+
device_name = f"Relay {serial_number}"
77+
entity_name = f"{device_name} {sensor_description.name}"
78+
79+
entities.append(
80+
EnvoyRelayGenericEntity(
81+
sensor_description,
82+
entity_name,
83+
device_name,
84+
serial_number,
85+
None,
86+
coordinator,
87+
config_entry.unique_id,
88+
)
89+
)
90+
6991
elif sensor_description.key == "firmware":
7092
if coordinator.data.get("update_status") is not None:
7193
entity_name = f"{name} {sensor_description.name}"
@@ -312,24 +334,65 @@ class EnvoyRelayEntity(EnvoyBinaryEntity):
312334

313335
MODEL = "Relay"
314336

337+
@property
338+
def relay(self):
339+
return self.coordinator.data.get("relays", {}).get(
340+
self._device_serial_number, {}
341+
)
342+
343+
@property
344+
def value_key(self):
345+
if "_" in self.entity_description.key:
346+
return self.entity_description.key.split("_", 1)[-1]
347+
return self.entity_description.key
348+
349+
@property
350+
def available(self) -> bool:
351+
"""Return if entity is available."""
352+
if not self.coordinator.last_update_success:
353+
return False
354+
355+
if self.value_key != "communicating":
356+
return self.relay.get("communicating")
357+
358+
return True
359+
315360
@property
316361
def is_on(self) -> bool | None:
317362
"""Return true if the binary sensor is on."""
318-
relays = self.coordinator.data.get("relays")
319-
if relays is None:
363+
if self.relay is None:
320364
return None
321365

322-
return relays.get(self._serial_number).get("relay") == "closed"
366+
return self.relay.get("relay") == "closed"
323367

324368
@property
325369
def extra_state_attributes(self) -> dict | None:
326370
"""Return the state attributes."""
327-
if self.coordinator.data.get("relays") is not None:
328-
relay = self.coordinator.data.get("relays").get(self._serial_number)
329-
return {
330-
"last_reported": relay.get("report_date"),
331-
"reason_code": relay.get("reason_code"),
332-
"reason": relay.get("reason"),
333-
}
371+
if self.relay is None:
372+
return None
373+
374+
return {
375+
"last_reported": self.relay.get("report_date"),
376+
"reason_code": self.relay.get("reason_code"),
377+
"reason": self.relay.get("reason"),
378+
}
379+
380+
381+
class EnvoyRelayContactEntity(EnvoyRelayEntity):
382+
@property
383+
def icon(self):
384+
return "mdi:electric-switch-closed" if self.is_on else "mdi:electric-switch"
334385

386+
387+
class EnvoyRelayGenericEntity(EnvoyRelayEntity):
388+
@property
389+
def is_on(self) -> bool | None:
390+
"""Return true if the binary sensor is on."""
391+
if self.relay is None:
392+
return None
393+
394+
return self.relay.get(self.value_key)
395+
396+
@property
397+
def extra_state_attributes(self) -> dict | None:
335398
return None

custom_components/enphase_envoy/const.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,19 @@
314314
),
315315
BinarySensorEntityDescription(
316316
key="relays",
317-
name="Relay",
317+
name="Contact",
318318
device_class=BinarySensorDeviceClass.POWER,
319319
),
320+
BinarySensorEntityDescription(
321+
key="relays_communicating",
322+
name="Communicating",
323+
device_class=BinarySensorDeviceClass.CONNECTIVITY,
324+
),
325+
BinarySensorEntityDescription(
326+
key="relays_forced",
327+
name="Forced",
328+
device_class=BinarySensorDeviceClass.TAMPER,
329+
),
320330
BinarySensorEntityDescription(
321331
key="firmware",
322332
name="Firmware",

custom_components/enphase_envoy/envoy_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def relays(self):
474474
)
475475
if not status:
476476
# fallback to the information which is available with owner token.
477-
status = self.relay_info
477+
status = self.get("relay_info")
478478
return status
479479

480480
@envoy_property(required_endpoint="endpoint_ensemble_json_results")

0 commit comments

Comments
 (0)