Skip to content

Commit

Permalink
Fix fetching climate state
Browse files Browse the repository at this point in the history
  • Loading branch information
basbruss committed Nov 28, 2023
1 parent 80b234f commit b81f5ae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
23 changes: 13 additions & 10 deletions custom_components/adaptive_cover/calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,11 @@ def climate_state(self):
# glare does not matter
if self.is_presence is False and self.temp is not None:
# allow maximum solar radiation
if self.temp < self.low_point:
temp = float(self.temp)
if temp < self.low_point:
return 100
# don't allow solar radiation
if self.temp > self.high_point:
if temp > self.high_point:
return 0
return self.default

Expand Down Expand Up @@ -416,17 +417,18 @@ def calculated_state(self):

def control_method_tilt_single(self):
"""Single direction control schema."""
temp = float(self.temp)
if self.is_presence is False:
if self.temp < self.low_point:
if temp < self.low_point:
return 100
if self.temp > self.high_point:
if temp > self.high_point:
return 0
# 80 degrees is optimal by no need to shield or use solar contribution
return 80 / 90 * 100
if self.is_presence:
if self.temp < self.low_point:
if temp < self.low_point:
return self.basic_state()
if self.temp > self.high_point:
if temp > self.high_point:
return 45 / 90 * 100
# 80 degrees is optimal by no need to shield or use solar contribution
if self.valid:
Expand All @@ -436,20 +438,21 @@ def control_method_tilt_single(self):
def control_method_tilt_bi(self):
"""bi-directional control schema."""
beta = np.rad2deg(self.beta)
temp = float(self.temp)
if self.is_presence is False:
if self.temp < self.low_point:
if temp < self.low_point:
# parallel to sun beams
if self.valid:
return (beta + 90) / 180 * 100
return 110 / 180 * 100
if self.temp > self.high_point:
if temp > self.high_point:
return 0
# 80 degrees is optimal by no need to shield or use solar contribution
return 80 / 180 * 100
if self.is_presence:
if self.temp < self.low_point:
if temp < self.low_point:
return self.basic_state()
if self.temp > self.high_point:
if temp > self.high_point:
return 45 / 180 * 100
# 80 degrees is optimal by no need to shield or use solar contribution
if self.valid:
Expand Down
21 changes: 14 additions & 7 deletions custom_components/adaptive_cover/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
CONF_MODE,
)

def get_safe_state(hass, entity_id: str):
"""Get a safe state value if not available."""
state = hass.states.get(entity_id)
if not state or state.state in ["unknown", "unavailable"]:
return None
return state.state

async def async_setup_entry(
hass: HomeAssistant,
Expand Down Expand Up @@ -120,14 +126,15 @@ async def async_added_to_hass(self) -> None:
def async_update_state(self) -> None:
"""Determine state after push."""
self._cover_data.update()
if self._mode == "climate":
self._cover_data.update_climate()
if self._cover_type == "cover_blind":
self._cover_data.update_vertical()
if self._cover_type == "cover_awning":
self._cover_data.update_horizontal()
if self._cover_type == "cover_tilt":
self._cover_data.update_tilt()
if self._mode == "climate":
self._cover_data.update_climate()


self.async_write_ha_state()

Expand Down Expand Up @@ -243,9 +250,9 @@ def update_vertical(self):
self.presence,
self.presence_entity,
)
self.calculated_state = values.basic_state()
if self._mode == "climate":
self.calculated_state = values.climate_state()
# self.calculated_state = values.basic_state()
# if self._mode == "climate":
self.calculated_state = values.climate_state()

def update_horizontal(self):
"""Update values for horizontal blind."""
Expand Down Expand Up @@ -317,10 +324,10 @@ def update_climate(self):
"current_temperature"
]
else:
self.current_temp = self.hass.states.get("sun.sun").state or None
self.current_temp = get_safe_state(self.hass, self.temp_entity)

if self.presence_entity is not None:
self.presence = self.hass.states.get("sun.sun").state or None
self.presence = get_safe_state(self.hass, self.presence_entity)

@property
def state(self):
Expand Down

0 comments on commit b81f5ae

Please sign in to comment.