Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Enphase switch platform and grid enable switch #98261

Merged
merged 4 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/enphase_envoy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

DOMAIN = "enphase_envoy"

PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]

INVALID_AUTH_ERRORS = (EnvoyAuthenticationError, EnvoyAuthenticationRequired)
2 changes: 1 addition & 1 deletion homeassistant/components/enphase_envoy/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"documentation": "https://www.home-assistant.io/integrations/enphase_envoy",
"iot_class": "local_polling",
"loggers": ["pyenphase"],
"requirements": ["pyenphase==1.3.0"],
"requirements": ["pyenphase==1.4.0"],
"zeroconf": [
{
"type": "_enphase-envoy._tcp.local."
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/enphase_envoy/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
"lifetime_consumption": {
"name": "Lifetime energy consumption"
}
},
"switch": {
"grid_enabled": {
"name": "Grid enabled"
}
}
}
}
113 changes: 113 additions & 0 deletions homeassistant/components/enphase_envoy/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""Switch platform for Enphase Envoy solar energy monitor."""
from __future__ import annotations

Check warning on line 2 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L2

Added line #L2 was not covered by tests

from collections.abc import Callable, Coroutine
from dataclasses import dataclass
import logging
from typing import Any

Check warning on line 7 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L4-L7

Added lines #L4 - L7 were not covered by tests

from pyenphase import Envoy, EnvoyEnpower

Check warning on line 9 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L9

Added line #L9 was not covered by tests

from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Check warning on line 15 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L11-L15

Added lines #L11 - L15 were not covered by tests

from .const import DOMAIN
from .coordinator import EnphaseUpdateCoordinator
from .entity import EnvoyBaseEntity

Check warning on line 19 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L17-L19

Added lines #L17 - L19 were not covered by tests

_LOGGER = logging.getLogger(__name__)

Check warning on line 21 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L21

Added line #L21 was not covered by tests


@dataclass
class EnvoyEnpowerRequiredKeysMixin:

Check warning on line 25 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L24-L25

Added lines #L24 - L25 were not covered by tests
"""Mixin for required keys."""

value_fn: Callable[[EnvoyEnpower], bool]
turn_on_fn: Callable[[Envoy], Coroutine[Any, Any, dict[str, Any]]]
turn_off_fn: Callable[[Envoy], Coroutine[Any, Any, dict[str, Any]]]

Check warning on line 30 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L28-L30

Added lines #L28 - L30 were not covered by tests


@dataclass
class EnvoyEnpowerSwitchEntityDescription(

Check warning on line 34 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L33-L34

Added lines #L33 - L34 were not covered by tests
SwitchEntityDescription, EnvoyEnpowerRequiredKeysMixin
):
"""Describes an Envoy Enpower switch entity."""


ENPOWER_GRID_SWITCH = EnvoyEnpowerSwitchEntityDescription(

Check warning on line 40 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L40

Added line #L40 was not covered by tests
key="mains_admin_state",
translation_key="grid_enabled",
value_fn=lambda enpower: enpower.mains_admin_state == "closed",
turn_on_fn=lambda envoy: envoy.go_on_grid(),
bdraco marked this conversation as resolved.
Show resolved Hide resolved
turn_off_fn=lambda envoy: envoy.go_off_grid(),
)


async def async_setup_entry(

Check warning on line 49 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L49

Added line #L49 was not covered by tests
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Enphase Envoy switch platform."""
coordinator: EnphaseUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
envoy_data = coordinator.envoy.data
assert envoy_data is not None
envoy_serial_num = config_entry.unique_id
assert envoy_serial_num is not None
entities: list[SwitchEntity] = []
if envoy_data.enpower:
entities.extend(

Check warning on line 62 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L55-L62

Added lines #L55 - L62 were not covered by tests
[
EnvoyEnpowerSwitchEntity(
coordinator, ENPOWER_GRID_SWITCH, envoy_data.enpower
)
]
)
async_add_entities(entities)

Check warning on line 69 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L69

Added line #L69 was not covered by tests


class EnvoyEnpowerSwitchEntity(EnvoyBaseEntity, SwitchEntity):

Check warning on line 72 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L72

Added line #L72 was not covered by tests
"""Representation of an Enphase Enpower switch entity."""

entity_description: EnvoyEnpowerSwitchEntityDescription

Check warning on line 75 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L75

Added line #L75 was not covered by tests

def __init__(

Check warning on line 77 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L77

Added line #L77 was not covered by tests
self,
coordinator: EnphaseUpdateCoordinator,
description: EnvoyEnpowerSwitchEntityDescription,
enpower: EnvoyEnpower,
) -> None:
"""Initialize the Enphase Enpower switch entity."""
super().__init__(coordinator, description)
self.envoy = coordinator.envoy
self.enpower = enpower
self._serial_number = enpower.serial_number
self._attr_unique_id = f"{self._serial_number}_{description.key}"
self._attr_device_info = DeviceInfo(

Check warning on line 89 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L84-L89

Added lines #L84 - L89 were not covered by tests
identifiers={(DOMAIN, self._serial_number)},
manufacturer="Enphase",
model="Enpower",
name=f"Enpower {self._serial_number}",
sw_version=str(enpower.firmware_version),
via_device=(DOMAIN, self.envoy_serial_num),
)

@property
def is_on(self) -> bool:

Check warning on line 99 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L98-L99

Added lines #L98 - L99 were not covered by tests
"""Return the state of the Enpower switch."""
enpower = self.data.enpower
assert enpower is not None
return self.entity_description.value_fn(enpower)

Check warning on line 103 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L101-L103

Added lines #L101 - L103 were not covered by tests

async def async_turn_on(self):

Check warning on line 105 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L105

Added line #L105 was not covered by tests
"""Turn on the Enpower switch."""
await self.entity_description.turn_on_fn(self.envoy)
await self.coordinator.async_request_refresh()

Check warning on line 108 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L107-L108

Added lines #L107 - L108 were not covered by tests

async def async_turn_off(self):

Check warning on line 110 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L110

Added line #L110 was not covered by tests
"""Turn off the Enpower switch."""
await self.entity_description.turn_off_fn(self.envoy)
await self.coordinator.async_request_refresh()

Check warning on line 113 in homeassistant/components/enphase_envoy/switch.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/enphase_envoy/switch.py#L112-L113

Added lines #L112 - L113 were not covered by tests
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ pyedimax==0.2.1
pyefergy==22.1.1

# homeassistant.components.enphase_envoy
pyenphase==1.3.0
pyenphase==1.4.0

# homeassistant.components.envisalink
pyenvisalink==4.6
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ pyeconet==0.1.20
pyefergy==22.1.1

# homeassistant.components.enphase_envoy
pyenphase==1.3.0
pyenphase==1.4.0

# homeassistant.components.everlights
pyeverlights==0.1.0
Expand Down