Skip to content

Commit

Permalink
fix dev branch
Browse files Browse the repository at this point in the history
  • Loading branch information
basbruss committed Mar 5, 2024
1 parent cd3d3d1 commit 29573ea
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 325 deletions.
11 changes: 7 additions & 4 deletions .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"github.vscode-pull-request-github",
"ryanluker.vscode-coverage-gutters",
"ms-python.vscode-pylance"
"charliermarsh.ruff",
"ms-python.pylint",
"ms-python.vscode-pylance",
"visualstudioexptteam.vscodeintellicode",
"redhat.vscode-yaml",
"esbenp.prettier-vscode",
"GitHub.vscode-pull-request-github"
],
"settings": {
"files.eol": "\n",
Expand Down
65 changes: 52 additions & 13 deletions custom_components/adaptive_cover/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
"""The Adaptive Cover integration."""

from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.event import (
async_track_entity_registry_updated_event,
async_track_state_change,
)

from .blueprint import configure_blueprint
from .const import (
CONF_CLIMATE_MODE,
CONF_PRESENCE_ENTITY,
CONF_TEMP_ENTITY,
CONF_WEATHER_ENTITY,
DOMAIN,
)
from .coordinator import AdaptiveDataUpdateCoordinator

PLATFORMS = [Platform.SENSOR, Platform.SWITCH]
CONF_ENTITIES = ["sun.sun"]


async def async_initialize_integration(
hass: HomeAssistant,
# *,
config_entry: ConfigEntry | None = None,
# config: dict[str, Any] | None = None,
) -> bool:
"""Initialize the integration."""

Expand All @@ -23,23 +37,48 @@ async def async_initialize_integration(
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Adaptive Cover from a config entry."""

await hass.config_entries.async_forward_entry_setups(entry, (Platform.SENSOR,))
hass.data.setdefault(DOMAIN, {})

await async_initialize_integration(hass=hass, config_entry=entry)
coordinator = AdaptiveDataUpdateCoordinator(hass)

entry.async_on_unload(entry.add_update_listener(config_entry_update_listener))
_climate_mode = entry.options.get(CONF_CLIMATE_MODE)
_temp_entity = entry.options.get(CONF_TEMP_ENTITY)
_presence_entity = entry.options.get(CONF_PRESENCE_ENTITY)
_weather_entity = entry.options.get(CONF_WEATHER_ENTITY)
_entities = ["sun.sun"]
for entity in [_temp_entity, _presence_entity, _weather_entity]:
if entity is not None:
_entities.append(entity)

return True
entry.async_on_unload(
async_track_state_change(
hass,
_entities,
coordinator.async_check_entity_state_change,
)
)

await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id] = coordinator

async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener, called when the config entry options are changed."""
await hass.config_entries.async_reload(entry.entry_id)
if _climate_mode:
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
else:
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])

entry.async_on_unload(entry.add_update_listener(_async_update_listener))
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, (Platform.SENSOR,)
):
return unload_ok
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok


async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)
71 changes: 47 additions & 24 deletions custom_components/adaptive_cover/calculation.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
"""Generate values for all types of covers."""
from datetime import timedelta, datetime

from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from datetime import datetime, timedelta

import numpy as np
import pandas as pd
from dataclasses import dataclass, field
from abc import ABC, abstractmethod
from numpy import cos, tan, sin
from homeassistant.core import HomeAssistant
from numpy import cos, sin, tan
from numpy import radians as rad

from .helpers import get_domain, get_safe_state
from .sun import SunData
from .helpers import get_domain


@dataclass
class AdaptiveGeneralCover(ABC):
"""Collect common data."""

hass: any
hass: HomeAssistant
sol_azi: float
sol_elev: float
sunset_pos: int
Expand Down Expand Up @@ -133,49 +137,65 @@ def get_state(self) -> int:
class ClimateCoverData:
"""Fetch additional data."""

temp: str
hass: HomeAssistant
temp_entity: str
temp_low: float
temp_high: float
presence: str
presence_entity: str
weather_state: str
weather_entity: str
weather_condition: list[str]
blind_type: str

@property
def current_temperature(self):
"""Get current temp from entity."""
if self.temp_entity is not None:
if get_domain(self.temp_entity) == "climate":
temp = self.hass.states.get(self.temp_entity).attributes[
"current_temperature"
]
temp = get_safe_state(self.hass, self.temp_entity)
return float(temp)
return -273

@property
def is_presence(self):
"""Checks if people are present."""
presence = None
if self.presence_entity is not None:
presence = get_safe_state(self.hass, self.presence_entity)
# set to true if no sensor is defined
if self.presence is not None:
if presence is not None:
domain = get_domain(self.presence_entity)
if domain == "device_tracker":
return self.presence == "home"
return presence == "home"
if domain == "zone":
return int(self.presence) > 0
return int(presence) > 0
if domain in ["binary_sensor", "input_boolean"]:
return self.presence == "on"
return presence == "on"
return True

@property
def current_temperature(self) -> float:
"""Convert state str to float."""
return float(self.temp)

@property
def is_winter(self) -> bool:
"""Check if temperature is below threshold."""
return self.current_temperature < self.temp_low
if self.temp_low:
return self.current_temperature < self.temp_low

@property
def is_summer(self) -> bool:
"""Check if temperature is over threshold."""
return self.current_temperature > self.temp_high
if self.temp_high:
return self.current_temperature > self.temp_high

@property
def is_sunny(self) -> bool:
"""Check if condition can contain radiation in winter."""
if self.weather_state in self.weather_condition:
return True
weather_state = None
if self.weather_entity is not None:
weather_state = get_safe_state(self.hass, self.weather_entity)
if self.weather_condition is not None:
if weather_state in self.weather_condition:
return True
return False


Expand All @@ -190,7 +210,7 @@ def normal_type_cover(self) -> int:
# glare does not matter
if (
self.climate_data.is_presence is False
and self.climate_data.temp is not None
and self.climate_data.current_temperature is not None
and self.cover.sol_elev > 0
):
# allow maximum solar radiation
Expand Down Expand Up @@ -251,7 +271,10 @@ def control_method_tilt_bi(self):

def tilt_state(self):
"""Add tilt specific controls."""
if self.climate_data.temp is not None and self.cover.sol_elev > 0:
if (
self.climate_data.current_temperature is not None
and self.cover.sol_elev > 0
):
if self.cover.mode == "mode1":
self.control_method_tilt_single()
if self.cover.mode == "mode2":
Expand Down
Loading

0 comments on commit 29573ea

Please sign in to comment.