-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Zeversolar integration #84887
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
Merged
frenck
merged 12 commits into
home-assistant:dev
from
kvanzuijlen:zeversolar-integration
Jan 3, 2023
Merged
Zeversolar integration #84887
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
11e652c
Initial integration for zeversolar
kvanzuijlen ee6481e
Added unit tests + updated dependencies
kvanzuijlen 8edc9de
Apply suggestions from code review
kvanzuijlen 2f1f8d1
Updated .coveragerc
kvanzuijlen 494ea99
Code improvements as requested in PR feedback
kvanzuijlen 9f55363
Fixed integrations.json and __init__.py
kvanzuijlen f63f9c3
Merge branch 'dev' into zeversolar-integration
kvanzuijlen 79faf27
Fixed unittests
kvanzuijlen 72ef30b
Added missing entry in .coveragerc
kvanzuijlen 37b53d6
Apply suggestions from code review
kvanzuijlen c1c8e62
PR feedback
kvanzuijlen 30b97c1
Removed whitespaces
kvanzuijlen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| """The Zeversolar integration.""" | ||
| from __future__ import annotations | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.core import HomeAssistant | ||
|
|
||
| from .const import DOMAIN, PLATFORMS | ||
| from .coordinator import ZeversolarCoordinator | ||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Set up Zeversolar from a config entry.""" | ||
| coordinator = ZeversolarCoordinator(hass=hass, entry=entry) | ||
| await coordinator.async_config_entry_first_refresh() | ||
|
|
||
| hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator | ||
| await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
| return True | ||
|
|
||
|
|
||
| async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Unload a config entry.""" | ||
| if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): | ||
| hass.data[DOMAIN].pop(entry.entry_id) | ||
| return unload_ok |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """Config flow for zeversolar integration.""" | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import Any | ||
|
|
||
| import voluptuous as vol | ||
| import zeversolar | ||
|
|
||
| from homeassistant import config_entries | ||
| from homeassistant.const import CONF_HOST | ||
| from homeassistant.data_entry_flow import FlowResult | ||
| from homeassistant.helpers import config_validation as cv | ||
|
|
||
| from .const import DOMAIN | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| STEP_USER_DATA_SCHEMA = vol.Schema( | ||
| { | ||
| vol.Required(CONF_HOST): cv.string, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): | ||
| """Handle a config flow for zeversolar.""" | ||
|
|
||
| VERSION = 1 | ||
|
|
||
| async def async_step_user( | ||
| self, user_input: dict[str, Any] | None = None | ||
| ) -> FlowResult: | ||
| """Handle the initial step.""" | ||
| if user_input is None: | ||
| return self.async_show_form( | ||
| step_id="user", data_schema=STEP_USER_DATA_SCHEMA | ||
| ) | ||
|
|
||
| errors = {} | ||
|
|
||
| client = zeversolar.ZeverSolarClient(host=user_input[CONF_HOST]) | ||
| try: | ||
| data = await self.hass.async_add_executor_job(client.get_data) | ||
| except zeversolar.ZeverSolarHTTPNotFound: | ||
| errors["base"] = "invalid_host" | ||
| except zeversolar.ZeverSolarHTTPError: | ||
| errors["base"] = "cannot_connect" | ||
| except zeversolar.ZeverSolarTimeout: | ||
| errors["base"] = "timeout_connect" | ||
| except Exception: # pylint: disable=broad-except | ||
| _LOGGER.exception("Unexpected exception") | ||
| errors["base"] = "unknown" | ||
| else: | ||
| await self.async_set_unique_id(data.serial_number) | ||
| self._abort_if_unique_id_configured() | ||
| return self.async_create_entry(title="Zeversolar", data=user_input) | ||
|
|
||
| return self.async_show_form( | ||
| step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """Constants for the zeversolar integration.""" | ||
|
|
||
| from homeassistant.const import Platform | ||
|
|
||
| DOMAIN = "zeversolar" | ||
|
|
||
| PLATFORMS = [ | ||
| Platform.SENSOR, | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| """Zeversolar coordinator.""" | ||
| from __future__ import annotations | ||
|
|
||
| from datetime import timedelta | ||
| import logging | ||
|
|
||
| import zeversolar | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import CONF_HOST | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.update_coordinator import DataUpdateCoordinator | ||
|
|
||
| from .const import DOMAIN | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ZeversolarCoordinator(DataUpdateCoordinator[zeversolar.ZeverSolarData]): | ||
| """Data update coordinator.""" | ||
|
|
||
| def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: | ||
| """Initialize the coordinator.""" | ||
| super().__init__( | ||
| hass, | ||
| _LOGGER, | ||
| name=DOMAIN, | ||
| update_interval=timedelta(minutes=1), | ||
| ) | ||
| self._client = zeversolar.ZeverSolarClient(host=entry.data[CONF_HOST]) | ||
|
|
||
| async def _async_update_data(self) -> zeversolar.ZeverSolarData: | ||
| """Fetch the latest data from the source.""" | ||
| return await self.hass.async_add_executor_job(self._client.get_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| """Base Entity for Zeversolar sensors.""" | ||
| from __future__ import annotations | ||
|
|
||
| from homeassistant.helpers.entity import DeviceInfo | ||
| from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
|
|
||
| from .const import DOMAIN | ||
| from .coordinator import ZeversolarCoordinator | ||
|
|
||
|
|
||
| class ZeversolarEntity( | ||
| CoordinatorEntity[ZeversolarCoordinator], | ||
| ): | ||
| """Defines a base Zeversolar entity.""" | ||
|
|
||
| _attr_has_entity_name = True | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| coordinator: ZeversolarCoordinator, | ||
| ) -> None: | ||
| """Initialize the Zeversolar entity.""" | ||
| super().__init__(coordinator=coordinator) | ||
| self._attr_device_info = DeviceInfo( | ||
| identifiers={(DOMAIN, coordinator.data.serial_number)}, | ||
| name="Zeversolar Sensor", | ||
| manufacturer="Zeversolar", | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "domain": "zeversolar", | ||
| "name": "Zeversolar", | ||
| "config_flow": true, | ||
| "documentation": "https://www.home-assistant.io/integrations/zeversolar", | ||
| "requirements": ["zeversolar==0.2.0"], | ||
| "codeowners": ["@kvanzuijlen"], | ||
| "iot_class": "local_polling", | ||
| "integration_type": "device" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """Support for the Zeversolar platform.""" | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| import zeversolar | ||
|
|
||
| from homeassistant.components.sensor import ( | ||
| SensorDeviceClass, | ||
| SensorEntity, | ||
| SensorEntityDescription, | ||
| SensorStateClass, | ||
| ) | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import UnitOfEnergy, UnitOfPower | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.entity import EntityCategory | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from .const import DOMAIN | ||
| from .coordinator import ZeversolarCoordinator | ||
| from .entity import ZeversolarEntity | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZeversolarEntityDescriptionMixin: | ||
| """Mixin for required keys.""" | ||
|
|
||
| value_fn: Callable[[zeversolar.ZeverSolarData], zeversolar.kWh | zeversolar.Watt] | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZeversolarEntityDescription( | ||
| SensorEntityDescription, ZeversolarEntityDescriptionMixin | ||
| ): | ||
| """Describes Zeversolar sensor entity.""" | ||
|
|
||
|
|
||
| SENSOR_TYPES = ( | ||
| ZeversolarEntityDescription( | ||
| key="pac", | ||
| name="Current power", | ||
| icon="mdi:solar-power-variant", | ||
| native_unit_of_measurement=UnitOfPower.WATT, | ||
| state_class=SensorStateClass.MEASUREMENT, | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| device_class=SensorDeviceClass.POWER, | ||
| value_fn=lambda data: data.pac, | ||
| ), | ||
| ZeversolarEntityDescription( | ||
| key="energy_today", | ||
| name="Energy today", | ||
| icon="mdi:home-battery", | ||
| native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, | ||
| state_class=SensorStateClass.TOTAL_INCREASING, | ||
| device_class=SensorDeviceClass.ENERGY, | ||
| value_fn=lambda data: data.energy_today, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
| ) -> None: | ||
| """Set up the Zeversolar sensor.""" | ||
| coordinator: ZeversolarCoordinator = hass.data[DOMAIN][entry.entry_id] | ||
| async_add_entities( | ||
| ZeversolarSensor( | ||
| description=description, | ||
| coordinator=coordinator, | ||
| ) | ||
| for description in SENSOR_TYPES | ||
| ) | ||
|
|
||
|
|
||
| class ZeversolarSensor(ZeversolarEntity, SensorEntity): | ||
| """Implementation of the Zeversolar sensor.""" | ||
|
|
||
| entity_description: ZeversolarEntityDescription | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| description: ZeversolarEntityDescription, | ||
| coordinator: ZeversolarCoordinator, | ||
| ) -> None: | ||
| """Initialize the sensor.""" | ||
| self.entity_description = description | ||
| super().__init__(coordinator=coordinator) | ||
| self._attr_unique_id = f"{coordinator.data.serial_number}_{description.key}" | ||
|
|
||
| @property | ||
| def native_value(self) -> int | float: | ||
| """Return sensor state.""" | ||
| return self.entity_description.value_fn(self.coordinator.data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "config": { | ||
| "step": { | ||
| "user": { | ||
| "data": { | ||
| "host": "[%key:common::config_flow::data::host%]" | ||
| } | ||
| } | ||
| }, | ||
| "error": { | ||
| "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", | ||
| "timeout_connect": "[%key:common::config_flow::error::timeout_connect%]", | ||
| "invalid_host": "[%key:common::config_flow::error::invalid_host%]", | ||
| "unknown": "[%key:common::config_flow::error::unknown%]" | ||
| }, | ||
| "abort": { | ||
| "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "config": { | ||
| "abort": { | ||
| "already_configured": "Device is already configured" | ||
| }, | ||
| "error": { | ||
| "cannot_connect": "Failed to connect", | ||
| "timeout_connect": "Timeout establishing connection", | ||
| "unknown": "Unexpected error" | ||
| }, | ||
| "step": { | ||
| "user": { | ||
| "data": { | ||
| "host": "Host" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -487,6 +487,7 @@ | |
| "youless", | ||
| "zamg", | ||
| "zerproc", | ||
| "zeversolar", | ||
| "zha", | ||
| "zwave_js", | ||
| "zwave_me", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Tests for the Zeversolar integration.""" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.