Skip to content
1 change: 0 additions & 1 deletion homeassistant/components/comfoconnect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class ComfoConnectBridge:

def __init__(self, hass, bridge, name, token, friendly_name, pin):
"""Initialize the ComfoConnect bridge."""
self.data = {}
self.name = name
self.hass = hass
self.unique_id = bridge.uuid.hex()
Expand Down
41 changes: 22 additions & 19 deletions homeassistant/components/comfoconnect/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import logging
import math
from typing import Any

from pycomfoconnect import (
CMD_FAN_MODE_AWAY,
Expand Down Expand Up @@ -38,18 +39,19 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the ComfoConnect fan platform."""
ccb = hass.data[DOMAIN]

add_entities([ComfoConnectFan(ccb.name, ccb)], True)
add_entities([ComfoConnectFan(ccb)], True)


class ComfoConnectFan(FanEntity):
"""Representation of the ComfoConnect fan platform."""

def __init__(self, name, ccb: ComfoConnectBridge) -> None:
current_speed = None

def __init__(self, ccb: ComfoConnectBridge) -> None:
"""Initialize the ComfoConnect fan."""
self._ccb = ccb
self._name = name

async def async_added_to_hass(self):
async def async_added_to_hass(self) -> None:
"""Register for sensor updates."""
_LOGGER.debug("Registering for fan speed")
self.async_on_remove(
Expand All @@ -68,7 +70,7 @@ def _handle_update(self, value):
_LOGGER.debug(
"Handle update for fan speed (%d): %s", SENSOR_FAN_SPEED_MODE, value
)
self._ccb.data[SENSOR_FAN_SPEED_MODE] = value
self.current_speed = value
self.schedule_update_ha_state()

@property
Expand All @@ -84,7 +86,7 @@ def unique_id(self):
@property
def name(self):
"""Return the name of the fan."""
return self._name
return self._ccb.name

@property
def icon(self):
Expand All @@ -99,39 +101,40 @@ def supported_features(self) -> int:
@property
def percentage(self) -> int | None:
"""Return the current speed percentage."""
speed = self._ccb.data.get(SENSOR_FAN_SPEED_MODE)
if speed is None:
if self.current_speed is None:
return None
return ranged_value_to_percentage(SPEED_RANGE, speed)
return ranged_value_to_percentage(SPEED_RANGE, self.current_speed)

@property
def speed_count(self) -> int:
"""Return the number of speeds the fan supports."""
return int_states_in_range(SPEED_RANGE)

def turn_on(
self, speed: str = None, percentage=None, preset_mode=None, **kwargs
self,
speed: str | None = None,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs,
) -> None:
"""Turn on the fan."""
self.set_percentage(percentage)
if percentage is None:
self.set_percentage(1) # Set fan speed to low
else:
self.set_percentage(percentage)

def turn_off(self, **kwargs) -> None:
def turn_off(self, **kwargs: Any) -> None:
"""Turn off the fan (to away)."""
self.set_percentage(0)

def set_percentage(self, percentage: int):
def set_percentage(self, percentage: int) -> None:
"""Set fan speed percentage."""
_LOGGER.debug("Changing fan speed percentage to %s", percentage)

if percentage is None:
cmd = CMD_FAN_MODE_LOW
elif percentage == 0:
if percentage == 0:
cmd = CMD_FAN_MODE_AWAY
else:
speed = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
cmd = CMD_MAPPING[speed]

self._ccb.comfoconnect.cmd_rmi_request(cmd)

# Update current mode
self.schedule_update_ha_state()
Loading