Skip to content
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

File reader as NP sensor (for development and debugging) #74

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add file-reader NP entity
  • Loading branch information
dala318 committed Jan 8, 2025
commit 72bc2afc0c70477806ecbbf30691c8b6c1304a72
38 changes: 36 additions & 2 deletions custom_components/nordpool_planner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

from __future__ import annotations

import contextlib
import datetime as dt
import json
import logging
import pathlib

from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
Expand All @@ -12,7 +15,7 @@
STATE_UNKNOWN,
Platform,
)
from homeassistant.core import HomeAssistant, HomeAssistantError
from homeassistant.core import HomeAssistant, HomeAssistantError, State
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import (
Expand Down Expand Up @@ -718,9 +721,40 @@ def current_price_attr(self):
return price["value"]
return None

def _get_np_from_file(self, data_file: str):
"""Fake NP entity from file."""
diag_data = {}
file_path = pathlib.Path(data_file)
if file_path.is_file():
with contextlib.suppress(ValueError):
diag_data = json.loads(file_path.read_text())

if data := diag_data.get("data"):
if planner := data.get("planner"):
if prices_entity := planner.get("_prices_entity"):
if np := prices_entity.get("_np"):
return State(
entity_id=np.get("entity_id"),
state=np.get("state"),
attributes=np.get("attributes"),
# last_changed: datetime.datetime | None = None,
# last_reported: datetime.datetime | None = None,
# last_updated: datetime.datetime | None = None,
# context: Context | None = None,
# validate_entity_id: bool | None = True,
# state_info: StateInfo | None = None,
# last_updated_timestamp: float | None = None,
)

return None

def update(self, hass: HomeAssistant) -> bool:
"""Update price in storage."""
np = hass.states.get(self._unique_id)
if self._unique_id == "file_reader":
np = self._get_np_from_file("config/config_entry-nordpool_planner.json")
else:
np = hass.states.get(self._unique_id)

if np is None:
_LOGGER.warning("Got empty data from Nordpool entity %s ", self._unique_id)
elif "today" not in np.attributes and "prices_today" not in np.attributes:
Expand Down
6 changes: 4 additions & 2 deletions custom_components/nordpool_planner/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ async def async_step_user(
if "nordpool" in s or "average_electricity_price" in s
]

if len(selected_entities) == 0:
errors["base"] = "No Nordpool entity found"
# if len(selected_entities) == 0:
# errors["base"] = "No Nordpool entity found"

selected_entities.append("file_reader")

schema = vol.Schema(
{
Expand Down