-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
- Loading branch information
1 parent
c1a6f83
commit 6349760
Showing
17 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.