Skip to content

Commit

Permalink
add control method sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
basbruss committed Mar 6, 2024
1 parent f2e3efe commit 293f086
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
16 changes: 7 additions & 9 deletions custom_components/adaptive_cover/calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,15 @@ class ClimateCoverData:
blind_type: str

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

@property
def is_presence(self):
Expand All @@ -178,15 +177,15 @@ def is_presence(self):
@property
def is_winter(self) -> bool:
"""Check if temperature is below threshold."""
if self.temp_low and self.current_temperature:
return self.current_temperature < self.temp_low
if self.temp_low is not None and self.current_temperature is not None:
return float(self.current_temperature) < self.temp_low
return False

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

@property
Expand All @@ -196,8 +195,7 @@ def is_sunny(self) -> bool:
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 weather_state in weather_state
return False


Expand Down
9 changes: 9 additions & 0 deletions custom_components/adaptive_cover/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,22 @@ async def _async_update_data(self) -> AdaptiveCoverData:
default_state = 100 - default_state
climate_state = 100 - climate_state

climate_data = ClimateCoverState(cover_data, climate).climate_data

control_method = "intermediate"
if climate_data.is_summer:
control_method = "summer"
if climate_data.is_winter:
control_method = "winter"

return AdaptiveCoverData(
climate_mode_toggle=self.switch_mode,
states={
"normal": default_state,
"climate": climate_state,
"start": NormalCoverState(cover_data).cover.solar_times()[0],
"end": NormalCoverState(cover_data).cover.solar_times()[1],
"control": control_method,
},
attributes={
"default": self.config_entry.options.get(CONF_DEFAULT_HEIGHT),
Expand Down
59 changes: 58 additions & 1 deletion custom_components/adaptive_cover/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ async def async_setup_entry(
"mdi:sun-clock",
coordinator,
)
async_add_entities([sensor, start, end])
control = AdaptiveCoverControlSensorEntity(
config_entry.entry_id, hass, config_entry, name, coordinator
)
async_add_entities([sensor, start, end, control])


class AdaptiveCoverSensorEntity(
Expand Down Expand Up @@ -181,3 +184,57 @@ def device_info(self) -> DeviceInfo:
identifiers={(DOMAIN, self.id)},
name=self._device_name,
)


class AdaptiveCoverControlSensorEntity(
CoordinatorEntity[AdaptiveDataUpdateCoordinator], SensorEntity
):
"""Adaptive Cover Control method Sensor."""

_attr_has_entity_name = True
_attr_should_poll = False
_attr_translation_key = "control"

def __init__(
self,
unique_id: str,
hass,
config_entry,
name: str,
coordinator: AdaptiveDataUpdateCoordinator,
) -> None:
"""Initialize adaptive_cover Sensor."""
super().__init__(coordinator=coordinator)
self.type = {
"cover_blind": "Vertical",
"cover_awning": "Horizontal",
"cover_tilt": "Tilt",
}
self.data = self.coordinator.data
self._sensor_name = "Control Method"
self._attr_unique_id = f"{unique_id}_{self._sensor_name}"
self.id = unique_id
self.hass = hass
self.config_entry = config_entry
self._name = name
self._cover_type = self.config_entry.data["sensor_type"]
self._device_name = self.type[config_entry.data[CONF_SENSOR_TYPE]]

@property
def name(self):
"""Name of the entity."""
return f"{self._sensor_name} {self._name}"

@property
def native_value(self) -> str | None:
"""Handle when entity is added."""
return self.data.states["control"]

@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, self.id)},
name=self._device_name,
)

0 comments on commit 293f086

Please sign in to comment.