Skip to content

Commit

Permalink
cve try 2
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-dcs committed Feb 15, 2025
1 parent 9a4c338 commit f9fdc92
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 deletions.
19 changes: 2 additions & 17 deletions custom_components/ithodaalderop/fans/base_fans.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,17 @@ def __init__(
) -> None:
"""Initialize the fan."""
self.entity_description = description
self._preset_mode = None

self.entity_description.translation_key = self.entity_description.key

self._attr_preset_modes = self.entity_description.preset_modes
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, get_entity_prefix(config_entry.data))},
manufacturer=MANUFACTURER,
model=get_device_model(config_entry.data),
name=get_device_name(config_entry.data),
)

self._attr_unique_id = (
f"{get_entity_prefix(config_entry.data)}_{description.key}".lower()
)
self.entity_id = f"fan.{self._attr_unique_id}"

@property
def preset_modes(self):
"""Return the available preset modes."""
return self.entity_description.preset_modes

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

@property
def preset_mode(self):
"""Return the current preset mode."""
return self._preset_mode
24 changes: 10 additions & 14 deletions custom_components/ithodaalderop/fans/cve_hru200.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def get_cve_hru200_fan(config_entry: ConfigEntry):
class IthoFanCVE_HRU200(IthoBaseFan):
"""Representation of an MQTT-controlled fan."""

_is_on = None

async def async_added_to_hass(self) -> None:
"""Subscribe to MQTT events."""
await mqtt.async_subscribe(
Expand All @@ -56,20 +54,22 @@ def _message_received(self, msg):
"""Handle preset mode update via MQTT."""
try:
data = json.loads(msg.payload)
speed = int(data.get("Ventilation level (%)", -1)) + int(
percentage = int(data.get("Ventilation level (%)", -1)) + int(
data.get("Ventilation setpoint (%)", -1)
)

self._is_on = speed > 0
self._attr_percentage = percentage
except ValueError:
self._is_on = None
self._attr_percentage = None

self.async_write_ha_state()

async def async_set_preset_mode(self, preset_mode):
"""Set the fan preset mode."""
if preset_mode in PRESET_MODES:
preset_command = PRESET_MODES[preset_mode]

payload = json.dumps({self.entity_description.command_key: preset_command})
# payload = json.dumps({self.entity_description.command_key: preset_command})
payload = json.dumps(preset_command)
await mqtt.async_publish(
self.hass,
self.entity_description.command_topic,
Expand All @@ -80,13 +80,14 @@ async def async_set_preset_mode(self, preset_mode):

async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan, as a percentage."""
payload = json.dumps({self.entity_description.command_key: percentage * 2.55})
# payload = json.dumps({self.entity_description.command_key: percentage * 2.55})
payload = json.dumps(int(percentage * 2.55))
await mqtt.async_publish(
self.hass,
self.entity_description.command_topic,
payload,
)
self.percentage = percentage
self._attr_percentage = percentage
self.async_write_ha_state()

async def async_turn_on(self, *args, **kwargs):
Expand All @@ -96,8 +97,3 @@ async def async_turn_on(self, *args, **kwargs):
async def async_turn_off(self, **kwargs):
"""Turn off the fan."""
await self.async_set_percentage(0)

@property
def is_on(self):
"""Return true if the fan is on."""
return self._is_on
5 changes: 2 additions & 3 deletions custom_components/ithodaalderop/fans/hru350.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def _message_received(self, msg):
data = json.loads(msg.payload)
actual_mode = int(data.get("Actual Mode", -1))

self._preset_mode = ACTUAL_MODES.get(actual_mode)

self._attr_preset_mode = ACTUAL_MODES.get(actual_mode)
self.async_write_ha_state()
except ValueError:
_LOGGER.error("Invalid JSON received for preset mode: %s", msg.payload)
Expand All @@ -84,7 +83,7 @@ async def async_set_preset_mode(self, preset_mode):
self.entity_description.command_topic,
payload,
)
self._preset_mode = preset_mode
self._attr_preset_mode = preset_mode
self.async_write_ha_state()
else:
_LOGGER.warning(f"Invalid preset mode: {preset_mode}")
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ithodaalderop/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"mqtt": ["ithohru/#"],
"requirements": [
],
"version": "2.5.0-b7"
"version": "2.5.0-b8"
}

0 comments on commit f9fdc92

Please sign in to comment.