Add Synchronize inverter clock button (#69220)

* Add Synchronize inverter clock button

* Use generic GoodweButtonEntityDescription

* Replace deprecated code

* Fix DT inverter export limit type

* Remove fix to DT inverter export limit time
This commit is contained in:
mletenay 2022-08-25 07:52:05 +02:00 committed by GitHub
parent cd7625d4a2
commit bbc2c28ef3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 1 deletions

View file

@ -443,6 +443,7 @@ omit =
homeassistant/components/glances/sensor.py
homeassistant/components/goalfeed/*
homeassistant/components/goodwe/__init__.py
homeassistant/components/goodwe/button.py
homeassistant/components/goodwe/const.py
homeassistant/components/goodwe/number.py
homeassistant/components/goodwe/select.py

View file

@ -0,0 +1,84 @@
"""GoodWe PV inverter selection settings entities."""
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from datetime import datetime
import logging
from goodwe import Inverter, InverterError
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN, KEY_DEVICE_INFO, KEY_INVERTER
_LOGGER = logging.getLogger(__name__)
@dataclass
class GoodweButtonEntityDescriptionRequired:
"""Required attributes of GoodweButtonEntityDescription."""
action: Callable[[Inverter], Awaitable[None]]
@dataclass
class GoodweButtonEntityDescription(
ButtonEntityDescription, GoodweButtonEntityDescriptionRequired
):
"""Class describing Goodwe button entities."""
SYNCHRONIZE_CLOCK = GoodweButtonEntityDescription(
key="synchronize_clock",
name="Synchronize inverter clock",
icon="mdi:clock-check-outline",
entity_category=EntityCategory.CONFIG,
action=lambda inv: inv.write_setting("time", datetime.now()),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the inverter button entities from a config entry."""
inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER]
device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO]
# read current time from the inverter
try:
await inverter.read_setting("time")
except (InverterError, ValueError):
# Inverter model does not support clock synchronization
_LOGGER.debug("Could not read inverter current clock time")
else:
async_add_entities(
[GoodweButtonEntity(device_info, SYNCHRONIZE_CLOCK, inverter)]
)
class GoodweButtonEntity(ButtonEntity):
"""Entity representing the inverter clock synchronization button."""
_attr_should_poll = False
entity_description: GoodweButtonEntityDescription
def __init__(
self,
device_info: DeviceInfo,
description: GoodweButtonEntityDescription,
inverter: Inverter,
) -> None:
"""Initialize the inverter operation mode setting entity."""
self.entity_description = description
self._attr_unique_id = f"{description.key}-{inverter.serial_number}"
self._attr_device_info = device_info
self._inverter: Inverter = inverter
async def async_press(self) -> None:
"""Triggers the button press service."""
await self.entity_description.action(self._inverter)

View file

@ -5,7 +5,7 @@ from homeassistant.const import Platform
DOMAIN = "goodwe"
PLATFORMS = [Platform.NUMBER, Platform.SELECT, Platform.SENSOR]
PLATFORMS = [Platform.BUTTON, Platform.NUMBER, Platform.SELECT, Platform.SENSOR]
DEFAULT_NAME = "GoodWe"
SCAN_INTERVAL = timedelta(seconds=10)