Skip to content

Commit

Permalink
Merge pull request #15 from SeitaBV/flexmeasures-review-changes
Browse files Browse the repository at this point in the history
Flexmeasures review changes
  • Loading branch information
victorgarcia98 authored Jan 29, 2025
2 parents 5fd3d41 + cc93ba0 commit b72b950
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 273 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,7 @@ omit =
homeassistant/components/zwave_me/switch.py
homeassistant/components/electrasmart/climate.py
homeassistant/components/electrasmart/__init__.py
homeassistant/components/flexmeasures/__init__.py
homeassistant/components/myuplink/__init__.py
homeassistant/components/myuplink/api.py
homeassistant/components/myuplink/application_credentials.py
Expand Down
51 changes: 27 additions & 24 deletions homeassistant/components/flexmeasures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
"""The FlexMeasures integration."""

from __future__ import annotations

from dataclasses import fields
import logging

from flexmeasures_client import FlexMeasuresClient
import isodate

# import isodate
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigValidationError
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .config_flow import get_host_and_ssl_from_url
from .const import DOMAIN, FRBC_CONFIG
from .control_types import FRBC_Config
from .services import (
async_setup_services,
async_unload_services,
Expand All @@ -29,41 +33,41 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up FlexMeasures from a config entry."""

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = entry
hass.data[DOMAIN]["current_id"] = entry.entry_id

config_data = dict(entry.data)
# use entry.data directly instead of the config_data dict
# config_data = dict(entry.data)
# Registers update listener to update config entry when options are updated.
unsub_options_update_listener = entry.add_update_listener(options_update_listener)
# unsub_options_update_listener = entry.add_update_listener(options_update_listener)
# Store a reference to the unsubscribe function to cleanup if an entry is unloaded.
config_data["unsub_options_update_listener"] = unsub_options_update_listener
host, ssl = get_host_and_ssl_from_url(config_data["url"])
# config_data["unsub_options_update_listener"] = unsub_options_update_listener

host, ssl = get_host_and_ssl_from_url(get_from_option_or_config("url", entry))
client = FlexMeasuresClient(
host=host,
email=config_data["username"],
password=config_data["password"],
email=get_from_option_or_config("username", entry),
password=get_from_option_or_config("password", entry),
ssl=ssl,
session=async_get_clientsession(hass),
)

# store config
hass.data[DOMAIN][FRBC_CONFIG] = {
"power_sensor_id": get_from_option_or_config("power_sensor", entry), # 1
"price_sensor_id": get_from_option_or_config(
"consumption_price_sensor", entry
), # 2
"soc_sensor_id": get_from_option_or_config("soc_sensor", entry), # 4
"rm_discharge_sensor_id": get_from_option_or_config(
"rm_discharge_sensor", entry
), # 5
"schedule_duration": isodate.parse_duration(
get_from_option_or_config("schedule_duration", entry)
), # PT24H
}
# if shcedule_duration is not set, throw an error
if get_from_option_or_config("schedule_duration", entry) is None:
raise ConfigValidationError(
message="Schedule duration is not set", exceptions=[]
)

frbc_data_dict = {}

for field in fields(FRBC_Config):
frbc_data_dict[field.name] = get_from_option_or_config(field.name, entry)

FRBC_data = FRBC_Config(**frbc_data_dict)
hass.data[DOMAIN][FRBC_CONFIG] = FRBC_data

hass.data[DOMAIN]["fm_client"] = client

hass.http.register_view(WebsocketAPIView())
hass.http.register_view(WebsocketAPIView(entry))

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand All @@ -81,7 +85,6 @@ async def options_update_listener(hass: HomeAssistant, config_entry: ConfigEntry

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""

if DOMAIN not in hass.data:
return True

Expand Down
Loading

0 comments on commit b72b950

Please sign in to comment.