2023-12-08 07:51:59 +00:00
|
|
|
"""Support for the OSO Energy devices and services."""
|
2024-03-08 14:04:07 +00:00
|
|
|
|
2023-12-08 07:51:59 +00:00
|
|
|
from typing import Any, Generic, TypeVar
|
|
|
|
|
|
|
|
from aiohttp.web_exceptions import HTTPException
|
|
|
|
from apyosoenergyapi import OSOEnergy
|
|
|
|
from apyosoenergyapi.helper.const import (
|
|
|
|
OSOEnergyBinarySensorData,
|
|
|
|
OSOEnergySensorData,
|
|
|
|
OSOEnergyWaterHeaterData,
|
|
|
|
)
|
|
|
|
from apyosoenergyapi.helper.osoenergy_exceptions import OSOEnergyReauthRequired
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_API_KEY, Platform
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers import aiohttp_client
|
2024-04-24 06:19:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2023-12-08 07:51:59 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
2024-04-24 06:19:26 +00:00
|
|
|
_OSOEnergyT = TypeVar(
|
|
|
|
"_OSOEnergyT",
|
|
|
|
OSOEnergyBinarySensorData,
|
|
|
|
OSOEnergySensorData,
|
|
|
|
OSOEnergyWaterHeaterData,
|
2023-12-08 07:51:59 +00:00
|
|
|
)
|
|
|
|
|
2024-04-24 06:19:26 +00:00
|
|
|
MANUFACTURER = "OSO Energy"
|
2023-12-08 07:51:59 +00:00
|
|
|
PLATFORMS = [
|
2024-04-24 06:19:26 +00:00
|
|
|
Platform.SENSOR,
|
2023-12-08 07:51:59 +00:00
|
|
|
Platform.WATER_HEATER,
|
|
|
|
]
|
|
|
|
PLATFORM_LOOKUP = {
|
2024-04-24 06:19:26 +00:00
|
|
|
Platform.SENSOR: "sensor",
|
2023-12-08 07:51:59 +00:00
|
|
|
Platform.WATER_HEATER: "water_heater",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up OSO Energy from a config entry."""
|
|
|
|
subscription_key = entry.data[CONF_API_KEY]
|
|
|
|
websession = aiohttp_client.async_get_clientsession(hass)
|
|
|
|
osoenergy = OSOEnergy(subscription_key, websession)
|
|
|
|
|
|
|
|
osoenergy_config = dict(entry.data)
|
|
|
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
|
|
|
try:
|
|
|
|
devices: Any = await osoenergy.session.start_session(osoenergy_config)
|
|
|
|
except HTTPException as error:
|
2024-03-17 23:40:38 +00:00
|
|
|
raise ConfigEntryNotReady from error
|
2023-12-08 07:51:59 +00:00
|
|
|
except OSOEnergyReauthRequired as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = osoenergy
|
|
|
|
|
|
|
|
platforms = set()
|
|
|
|
for ha_type, oso_type in PLATFORM_LOOKUP.items():
|
|
|
|
device_list = devices.get(oso_type, [])
|
|
|
|
if device_list:
|
|
|
|
platforms.add(ha_type)
|
|
|
|
if platforms:
|
|
|
|
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."""
|
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
2024-04-24 06:19:26 +00:00
|
|
|
class OSOEnergyEntity(Entity, Generic[_OSOEnergyT]):
|
2023-12-08 07:51:59 +00:00
|
|
|
"""Initiate OSO Energy Base Class."""
|
|
|
|
|
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2024-04-24 06:19:26 +00:00
|
|
|
def __init__(self, osoenergy: OSOEnergy, entity_data: _OSOEnergyT) -> None:
|
2023-12-08 07:51:59 +00:00
|
|
|
"""Initialize the instance."""
|
|
|
|
self.osoenergy = osoenergy
|
2024-04-24 06:19:26 +00:00
|
|
|
self.entity_data = entity_data
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, entity_data.device_id)},
|
|
|
|
manufacturer=MANUFACTURER,
|
|
|
|
model=entity_data.device_type,
|
|
|
|
name=entity_data.device_name,
|
|
|
|
)
|