-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to DeviceEntryType instead of entry_type
- Loading branch information
1 parent
71e75c5
commit 2a19c0e
Showing
7 changed files
with
287 additions
and
216 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,42 @@ | ||
"""The Alpha ESS integration.""" | ||
"""The AlphaEss integration.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import ConfigEntryNotReady | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
from homeassistant.helpers.update_coordinator import ( | ||
DataUpdateCoordinator, | ||
UpdateFailed, | ||
) | ||
|
||
from datetime import timedelta | ||
import logging | ||
|
||
import asyncio | ||
from alphaess import alphaess | ||
|
||
from .const import ( | ||
DOMAIN, | ||
STARTUP_MESSAGE | ||
) | ||
|
||
|
||
PLATFORMS = ["sensor"] | ||
|
||
SCAN_INTERVAL = timedelta(minutes=5) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME | ||
from homeassistant.core import HomeAssistant | ||
|
||
_LOGGER: logging.Logger = logging.getLogger(__package__) | ||
from .const import DOMAIN, PLATFORMS | ||
from .coordinator import AlphaESSDataUpdateCoordinator | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Set up Alpha ESS from a config entry.""" | ||
|
||
if hass.data.get(DOMAIN) is None: | ||
hass.data.setdefault(DOMAIN, {}) | ||
_LOGGER.info(STARTUP_MESSAGE) | ||
|
||
client = alphaess.alphaess() | ||
client.username = entry.data.get("username") | ||
client.password = entry.data.get("password") | ||
client.username = entry.data[CONF_USERNAME] | ||
client.password = entry.data[CONF_PASSWORD] | ||
|
||
coordinator = AlphaESSDataUpdateCoordinator(hass, client=client) | ||
await coordinator.async_config_entry_first_refresh() | ||
|
||
if not coordinator.last_update_success: | ||
raise ConfigEntryNotReady | ||
|
||
|
||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator | ||
|
||
hass.config_entries.async_setup_platforms(entry, PLATFORMS) | ||
|
||
entry.async_on_unload(entry.add_update_listener(async_update_options)) | ||
entry.async_on_unload(entry.add_update_listener(update_listener)) | ||
|
||
return True | ||
|
||
class AlphaESSDataUpdateCoordinator(DataUpdateCoordinator): | ||
"""Class to manage fetching data from the API.""" | ||
|
||
def __init__( | ||
self, hass: HomeAssistant, client: alphaess.alphaess | ||
) -> None: | ||
"""Initialize.""" | ||
self.api = client | ||
self.platforms = ['sensor'] | ||
self.update_method = self._async_update_data | ||
|
||
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL) | ||
|
||
async def _async_update_data(self): | ||
"""Update data via library.""" | ||
try: | ||
_LOGGER.info("Trying to query AlphaESS api data") | ||
return await self.api.getdata() | ||
except Exception as exception: | ||
raise UpdateFailed() from exception | ||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Unload a config entry.""" | ||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) | ||
if unload_ok: | ||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): | ||
hass.data[DOMAIN].pop(entry.entry_id) | ||
|
||
return unload_ok | ||
|
||
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None: | ||
"""Update options.""" | ||
await hass.config_entries.async_reload(entry.entry_id) | ||
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: | ||
"""Handle options update.""" | ||
await hass.config_entries.async_reload(entry.entry_id) |
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,52 @@ | ||
"""Coordinator for AlphaEss integration.""" | ||
import datetime | ||
import json | ||
import logging | ||
|
||
import aiohttp | ||
from alphaess import alphaess | ||
|
||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
from .const import DOMAIN, SCAN_INTERVAL | ||
|
||
_LOGGER: logging.Logger = logging.getLogger(__package__) | ||
|
||
|
||
class AlphaESSDataUpdateCoordinator(DataUpdateCoordinator): | ||
"""Class to manage fetching data from the API.""" | ||
|
||
def __init__(self, hass: HomeAssistant, client: alphaess.alphaess) -> None: | ||
"""Initialize.""" | ||
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL) | ||
self.api = client | ||
self.update_method = self._async_update_data | ||
self.data: dict[str, dict[str, float]] = {} | ||
|
||
async def _async_update_data(self): | ||
"""Update data via library.""" | ||
try: | ||
jsondata: json = await self.api.getdata() | ||
for invertor in jsondata: | ||
index = int(datetime.date.today().strftime("%d")) - 1 | ||
inverterdata: dict[str, any] = {} | ||
inverterdata.update({"Model": invertor["minv"]}) | ||
inverterdata.update({"Solar Production": invertor["statistics"]["EpvT"]}) | ||
inverterdata.update({"Solar to Battery": invertor["statistics"]["Epvcharge"]}) | ||
inverterdata.update({"Solar to Grid": invertor["statistics"]["Eout"]}) | ||
inverterdata.update({"Solar to Load": invertor["statistics"]["Epv2load"]}) | ||
inverterdata.update({"Total Load": invertor["statistics"]["EHomeLoad"]}) | ||
inverterdata.update({"Grid to Load": invertor["statistics"]["EGrid2Load"]}) | ||
inverterdata.update({"Grid to Battery": invertor["statistics"]["EGridCharge"]}) | ||
inverterdata.update({"State of Charge": invertor["statistics"]["Soc"]}) | ||
inverterdata.update({"Charge": invertor["system_statistics"]["ECharge"][index]}) | ||
inverterdata.update({"Discharge": invertor["system_statistics"]["EDischarge"][index]}) | ||
inverterdata.update({"EV Charger": invertor["statistics"]["EChargingPile"]}) | ||
self.data.update({invertor["sys_sn"]: inverterdata}) | ||
return self.data | ||
except ( | ||
aiohttp.client_exceptions.ClientConnectorError, | ||
aiohttp.ClientResponseError, | ||
) as error: | ||
raise UpdateFailed(error) from error |
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,16 @@ | ||
"""Parent class for AlphaESS devices.""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Callable | ||
from dataclasses import dataclass | ||
|
||
from homeassistant.components.sensor import SensorEntityDescription | ||
|
||
|
||
@dataclass | ||
class AlphaESSSensorDescription(SensorEntityDescription): | ||
"""Class to describe an AlphaESS sensor.""" | ||
|
||
native_value: Callable[ | ||
[str | int | float], str | int | float | ||
] | None = lambda val: val |
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 @@ | ||
"""Parent class for AlphaESSNames enum.""" | ||
from enum import Enum, unique | ||
|
||
|
||
@unique | ||
class AlphaESSNames(str, Enum): | ||
"""Device names used by AlphaESS.""" | ||
|
||
SolarProduction = "Solar Production" | ||
SolarToBattery = "Solar to Battery" | ||
SolarToGrid = "Solar to Grid" | ||
SolarToLoad = "Solar to Load" | ||
TotalLoad = "Total Load" | ||
GridToLoad = "Grid to Load" | ||
GridToBattery = "Grid to Battery" | ||
StateOfCharge = "State of Charge" | ||
Charge = "Charge" | ||
Discharge = "Discharge" | ||
EVCharger = "EV Charger" |
Oops, something went wrong.