Skip to content

Commit

Permalink
Ventilation Fan entity
Browse files Browse the repository at this point in the history
  • Loading branch information
signalkraft committed Nov 4, 2023
1 parent 7c67219 commit c0d5875
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 183 deletions.
152 changes: 0 additions & 152 deletions custom_components/mypyllant/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
HVACMode,
)
from homeassistant.components.climate.const import (
FAN_AUTO,
FAN_LOW,
FAN_OFF,
FAN_ON,
PRESET_AWAY,
PRESET_BOOST,
PRESET_NONE,
Expand All @@ -36,9 +32,6 @@
)
from myPyllant.models import (
System,
Ventilation,
VentilationFanStageType,
VentilationOperationMode,
Zone,
ZoneCurrentSpecialFunction,
ZoneHeatingOperatingMode,
Expand All @@ -56,7 +49,6 @@
SERVICE_SET_HOLIDAY,
SERVICE_SET_MANUAL_MODE_SETPOINT,
SERVICE_SET_QUICK_VETO,
SERVICE_SET_VENTILATION_FAN_STAGE,
SERVICE_SET_ZONE_TIME_PROGRAM,
)

Expand All @@ -80,23 +72,6 @@
PRESET_SLEEP: ZoneCurrentSpecialFunction.SYSTEM_OFF,
}

VENTILATION_HVAC_MODE_MAP = {
HVACMode.FAN_ONLY: VentilationOperationMode.NORMAL,
HVACMode.AUTO: VentilationOperationMode.TIME_CONTROLLED,
}

VENTILATION_FAN_MODE_MAP = {
FAN_OFF: VentilationOperationMode.OFF,
FAN_ON: VentilationOperationMode.NORMAL,
FAN_LOW: VentilationOperationMode.REDUCED,
FAN_AUTO: VentilationOperationMode.TIME_CONTROLLED,
}

_FAN_STAGE_TYPE_OPTIONS = [
selector.SelectOptionDict(value=v.value, label=v.value.title())
for v in VentilationFanStageType
]


async def async_setup_entry(
hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback
Expand All @@ -116,7 +91,6 @@ async def async_setup_entry(
return

zone_entities: list[ClimateEntity] = []
ventilation_entities: list[ClimateEntity] = []

for index, system in enumerate(coordinator.data):
for zone_index, _ in enumerate(system.zones):
Expand All @@ -129,17 +103,8 @@ async def async_setup_entry(
time_program_overwrite,
)
)
for ventilation_index, _ in enumerate(system.ventilation):
ventilation_entities.append(
VentilationClimate(
index,
ventilation_index,
coordinator,
)
)

async_add_entities(zone_entities)
async_add_entities(ventilation_entities)

if len(zone_entities) > 0:
platform = entity_platform.async_get_current_platform()
Expand Down Expand Up @@ -216,27 +181,6 @@ async def async_setup_entry(
"set_zone_time_program",
)

if len(ventilation_entities) > 0:
platform = entity_platform.async_get_current_platform()
_LOGGER.debug("Setting up ventilation climate entity services for %s", platform)
# noinspection PyTypeChecker
# Wrapping the schema in vol.Schema() breaks entity_id passing
platform.async_register_entity_service(
SERVICE_SET_VENTILATION_FAN_STAGE,
{
vol.Required("maximum_fan_stage"): vol.All(
vol.Coerce(int), vol.Clamp(min=1, max=6)
),
vol.Required("fan_stage_type"): selector.SelectSelector(
selector.SelectSelectorConfig(
options=_FAN_STAGE_TYPE_OPTIONS,
mode=selector.SelectSelectorMode.LIST,
),
),
},
"set_ventilation_fan_stage",
)


class ZoneClimate(CoordinatorEntity, ClimateEntity):
"""Climate for a zone."""
Expand Down Expand Up @@ -476,99 +420,3 @@ async def async_set_preset_mode(self, preset_mode):
await self.async_set_hvac_mode(HVACMode.OFF)

await self.coordinator.async_request_refresh_delayed()


class VentilationClimate(CoordinatorEntity, ClimateEntity):
coordinator: SystemCoordinator
_attr_fan_modes = [str(k) for k in VENTILATION_FAN_MODE_MAP.keys()]
_attr_hvac_modes = [str(k) for k in VENTILATION_HVAC_MODE_MAP.keys()]
_attr_temperature_unit = UnitOfTemperature.CELSIUS

def __init__(
self,
system_index: int,
ventilation_index: int,
coordinator: SystemCoordinator,
) -> None:
super().__init__(coordinator)
self.system_index = system_index
self.ventilation_index = ventilation_index
self.entity_id = f"{DOMAIN}.ventilation_{ventilation_index}"

@property
def system(self) -> System:
return self.coordinator.data[self.system_index]

@property
def ventilation(self) -> Ventilation:
return self.system.ventilation[self.ventilation_index]

@property
def device_info(self) -> DeviceInfo:
return DeviceInfo(
identifiers={(DOMAIN, f"ventilation{self.ventilation.index}")},
name=self.name,
manufacturer=self.system.brand_name,
)

@property
def unique_id(self) -> str:
return f"{DOMAIN}_climate_ventilation_{self.ventilation_index}"

@property
def name(self) -> str:
return [d for d in self.system.devices if d.type == "ventilation"][
0
].name_display

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
attr = {
"time_program_ventilation": self.ventilation.time_program_ventilation,
}
return attr

@property
def supported_features(self) -> ClimateEntityFeature:
"""Return the list of supported features."""
return ClimateEntityFeature.FAN_MODE

@property
def hvac_mode(self) -> HVACMode:
return [
k
for k, v in VENTILATION_HVAC_MODE_MAP.items()
if v == self.ventilation.operation_mode_ventilation
][0]

async def async_set_hvac_mode(self, hvac_mode):
await self.coordinator.api.set_ventilation_operation_mode(
self.ventilation,
VENTILATION_HVAC_MODE_MAP[hvac_mode],
)
await self.coordinator.async_request_refresh_delayed()

@property
def fan_mode(self) -> HVACMode:
return [
k
for k, v in VENTILATION_FAN_MODE_MAP.items()
if v == self.ventilation.operation_mode_ventilation
][0]

async def async_set_fan_mode(self, fan_mode: str) -> None:
await self.coordinator.api.set_ventilation_operation_mode(
self.ventilation,
VENTILATION_FAN_MODE_MAP[fan_mode],
)
await self.coordinator.async_request_refresh_delayed()

async def set_ventilation_fan_stage(
self, maximum_fan_stage: int | str, **kwargs: Any
) -> None:
await self.coordinator.api.set_ventilation_fan_stage(
self.ventilation,
int(maximum_fan_stage),
VentilationFanStageType(kwargs.get("fan_stage_type")),
)
await self.coordinator.async_request_refresh_delayed()
Loading

0 comments on commit c0d5875

Please sign in to comment.