1 """Support for Enphase Envoy solar energy monitor."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from operator
import attrgetter
9 from pyenphase
import EnvoyEncharge, EnvoyEnpower
12 BinarySensorDeviceClass,
14 BinarySensorEntityDescription,
21 from .const
import DOMAIN
22 from .coordinator
import EnphaseConfigEntry, EnphaseUpdateCoordinator
23 from .entity
import EnvoyBaseEntity
26 @dataclass(frozen=True, kw_only=True)
28 """Describes an Envoy Encharge binary sensor entity."""
30 value_fn: Callable[[EnvoyEncharge], bool]
36 translation_key=
"communicating",
37 device_class=BinarySensorDeviceClass.CONNECTIVITY,
38 entity_category=EntityCategory.DIAGNOSTIC,
39 value_fn=attrgetter(
"communicating"),
43 translation_key=
"dc_switch",
44 entity_category=EntityCategory.DIAGNOSTIC,
45 value_fn=
lambda encharge:
not encharge.dc_switch_off,
50 @dataclass(frozen=True, kw_only=True)
52 """Describes an Envoy Enpower binary sensor entity."""
54 value_fn: Callable[[EnvoyEnpower], bool]
60 translation_key=
"communicating",
61 device_class=BinarySensorDeviceClass.CONNECTIVITY,
62 entity_category=EntityCategory.DIAGNOSTIC,
63 value_fn=attrgetter(
"communicating"),
66 key=
"mains_oper_state",
67 translation_key=
"grid_status",
68 icon=
"mdi:transmission-tower",
69 value_fn=
lambda enpower: enpower.mains_oper_state ==
"closed",
76 config_entry: EnphaseConfigEntry,
77 async_add_entities: AddEntitiesCallback,
79 """Set up envoy binary sensor platform."""
80 coordinator = config_entry.runtime_data
81 envoy_data = coordinator.envoy.data
82 assert envoy_data
is not None
83 entities: list[BinarySensorEntity] = []
84 if envoy_data.encharge_inventory:
87 for description
in ENCHARGE_SENSORS
88 for encharge
in envoy_data.encharge_inventory
91 if envoy_data.enpower:
94 for description
in ENPOWER_SENSORS
101 """Defines a base envoy binary_sensor entity."""
104 class EnvoyEnchargeBinarySensorEntity(EnvoyBaseBinarySensorEntity):
105 """Defines an Encharge binary_sensor entity."""
107 entity_description: EnvoyEnchargeBinarySensorEntityDescription
111 coordinator: EnphaseUpdateCoordinator,
112 description: EnvoyEnchargeBinarySensorEntityDescription,
115 """Init the Encharge base entity."""
116 super().
__init__(coordinator, description)
119 encharge_inventory = self.
datadatadata.encharge_inventory
120 assert encharge_inventory
is not None
122 identifiers={(DOMAIN, serial_number)},
123 manufacturer=
"Enphase",
125 name=f
"Encharge {serial_number}",
126 sw_version=
str(encharge_inventory[self.
_serial_number_serial_number].firmware_version),
132 """Return the state of the Encharge binary_sensor."""
133 encharge_inventory = self.
datadatadata.encharge_inventory
134 assert encharge_inventory
is not None
139 """Defines an Enpower binary_sensor entity."""
141 entity_description: EnvoyEnpowerBinarySensorEntityDescription
145 coordinator: EnphaseUpdateCoordinator,
146 description: EnvoyEnpowerBinarySensorEntityDescription,
148 """Init the Enpower base entity."""
149 super().
__init__(coordinator, description)
151 assert enpower
is not None
154 identifiers={(DOMAIN, enpower.serial_number)},
155 manufacturer=
"Enphase",
157 name=f
"Enpower {enpower.serial_number}",
158 sw_version=
str(enpower.firmware_version),
164 """Return the state of the Enpower binary_sensor."""
166 assert enpower
is not None
None __init__(self, EnphaseUpdateCoordinator coordinator, EnvoyEnchargeBinarySensorEntityDescription description, str serial_number)
None __init__(self, EnphaseUpdateCoordinator coordinator, EnvoyEnpowerBinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, EnphaseConfigEntry config_entry, AddEntitiesCallback async_add_entities)