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 switch platform to V2C #103678

Merged
merged 6 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,7 @@ omit =
homeassistant/components/v2c/coordinator.py
homeassistant/components/v2c/entity.py
homeassistant/components/v2c/sensor.py
homeassistant/components/v2c/switch.py
homeassistant/components/velbus/__init__.py
homeassistant/components/velbus/binary_sensor.py
homeassistant/components/velbus/button.py
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/v2c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .const import DOMAIN
from .coordinator import V2CUpdateCoordinator

PLATFORMS: list[Platform] = [Platform.SENSOR]
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.SWITCH]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/v2c/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

SCAN_INTERVAL = timedelta(seconds=60)
SCAN_INTERVAL = timedelta(seconds=5)

_LOGGER = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/v2c/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/v2c",
"iot_class": "local_polling",
"requirements": ["pytrydan==0.1.2"]
"requirements": ["pytrydan==0.3.0"]
}
5 changes: 5 additions & 0 deletions homeassistant/components/v2c/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
}
},
"entity": {
"switch": {
dgomes marked this conversation as resolved.
Show resolved Hide resolved
"paused": {
"name": "Pause Session"
dgomes marked this conversation as resolved.
Show resolved Hide resolved
}
},
"sensor": {
"charge_power": {
"name": "Charge power"
Expand Down
93 changes: 93 additions & 0 deletions homeassistant/components/v2c/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""Switch platform for Enphase Envoy solar energy monitor."""
dgomes marked this conversation as resolved.
Show resolved Hide resolved
from __future__ import annotations

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

from pytrydan import Trydan, TrydanData
from pytrydan.models.trydan import PauseState

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

from .const import DOMAIN
from .coordinator import V2CUpdateCoordinator
from .entity import V2CBaseEntity

_LOGGER = logging.getLogger(__name__)


@dataclass
class V2CRequiredKeysMixin:
"""Mixin for required keys."""

value_fn: Callable[[TrydanData], bool]
turn_on_fn: Callable[[Trydan], Coroutine[Any, Any, Any]]
turn_off_fn: Callable[[Trydan], Coroutine[Any, Any, Any]]


@dataclass
class V2CSwitchEntityDescription(SwitchEntityDescription, V2CRequiredKeysMixin):
"""Describes an V2C EVSE switch entity."""
dgomes marked this conversation as resolved.
Show resolved Hide resolved


TRYDAN_SWITCHES = (
V2CSwitchEntityDescription(
key="paused",
translation_key="paused",
icon="mdi:pause",
value_fn=lambda evse_data: evse_data.paused == PauseState.PAUSED,
turn_on_fn=lambda evse: evse.pause(),
turn_off_fn=lambda evse: evse.resume(),
),
)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up V2C switch platform."""
coordinator: V2CUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]

entities: list[SwitchEntity] = [
V2CSwitchEntity(coordinator, description, config_entry.entry_id)
for description in TRYDAN_SWITCHES
]
async_add_entities(entities)


class V2CSwitchEntity(V2CBaseEntity, SwitchEntity):
"""Representation of a V2C switch entity."""

entity_description: V2CSwitchEntityDescription

def __init__(
self,
coordinator: V2CUpdateCoordinator,
description: SwitchEntityDescription,
entry_id: str,
) -> None:
"""Initialize the V2C switch entity."""
super().__init__(coordinator, description)
self._attr_unique_id = f"{entry_id}_{description.key}"

@property
def is_on(self) -> bool:
"""Return the state of the Enpower switch."""
dgomes marked this conversation as resolved.
Show resolved Hide resolved
return self.entity_description.value_fn(self.data)

async def async_turn_on(self):
"""Turn on the EVSE switch."""
await self.entity_description.turn_on_fn(self.coordinator.evse)
await self.coordinator.async_request_refresh()

async def async_turn_off(self):
"""Turn off the EVSE switch."""
await self.entity_description.turn_off_fn(self.coordinator.evse)
await self.coordinator.async_request_refresh()
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,7 @@ pytradfri[async]==9.0.1
pytrafikverket==0.3.8

# homeassistant.components.v2c
pytrydan==0.1.2
pytrydan==0.3.0

# homeassistant.components.usb
pyudev==0.23.2
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ pytradfri[async]==9.0.1
pytrafikverket==0.3.8

# homeassistant.components.v2c
pytrydan==0.1.2
pytrydan==0.3.0

# homeassistant.components.usb
pyudev==0.23.2
Expand Down