mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Add fan entity to Zimi integration (#144327)
* Import fan.py * Align to light design * Consistency for debug message * ruff (post merge) * Fixed stale docstring * refactor init * Combine aysnc_add_entities Use _attr_speed_range instead of property * Remove unused self._speed (and useless init) * Refactor self._device to device in Entity init
This commit is contained in:
parent
4271d3f32f
commit
e69b3ebf1e
@ -16,7 +16,7 @@ from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
|||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .helpers import async_connect_to_controller
|
from .helpers import async_connect_to_controller
|
||||||
|
|
||||||
PLATFORMS = [Platform.LIGHT, Platform.SWITCH]
|
PLATFORMS = [Platform.FAN, Platform.LIGHT, Platform.SWITCH]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -25,19 +25,19 @@ class ZimiEntity(Entity):
|
|||||||
"""Initialize an HA Entity which is a ZimiDevice."""
|
"""Initialize an HA Entity which is a ZimiDevice."""
|
||||||
|
|
||||||
self._device = device
|
self._device = device
|
||||||
self._attr_unique_id = self._device.identifier
|
self._attr_unique_id = device.identifier
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, self._device.manufacture_info.identifier)},
|
identifiers={(DOMAIN, device.manufacture_info.identifier)},
|
||||||
manufacturer=self._device.manufacture_info.manufacturer,
|
manufacturer=device.manufacture_info.manufacturer,
|
||||||
model=self._device.manufacture_info.model,
|
model=device.manufacture_info.model,
|
||||||
name=self._device.manufacture_info.name,
|
name=device.manufacture_info.name,
|
||||||
hw_version=device.manufacture_info.hwVersion,
|
hw_version=device.manufacture_info.hwVersion,
|
||||||
sw_version=device.manufacture_info.firmwareVersion,
|
sw_version=device.manufacture_info.firmwareVersion,
|
||||||
suggested_area=device.room,
|
suggested_area=device.room,
|
||||||
via_device=(DOMAIN, api.mac),
|
via_device=(DOMAIN, api.mac),
|
||||||
)
|
)
|
||||||
self._attr_name = self._device.name.strip()
|
self._attr_name = device.name.strip()
|
||||||
self._attr_suggested_area = self._device.room
|
self._attr_suggested_area = device.room
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
|
94
homeassistant/components/zimi/fan.py
Normal file
94
homeassistant/components/zimi/fan.py
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
"""Platform for fan integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import math
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.fan import FanEntity, FanEntityFeature
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
from homeassistant.util.percentage import (
|
||||||
|
percentage_to_ranged_value,
|
||||||
|
ranged_value_to_percentage,
|
||||||
|
)
|
||||||
|
from homeassistant.util.scaling import int_states_in_range
|
||||||
|
|
||||||
|
from . import ZimiConfigEntry
|
||||||
|
from .entity import ZimiEntity
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ZimiConfigEntry,
|
||||||
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up the Zimi Fan platform."""
|
||||||
|
|
||||||
|
api = config_entry.runtime_data
|
||||||
|
|
||||||
|
async_add_entities([ZimiFan(device, api) for device in api.fans])
|
||||||
|
|
||||||
|
|
||||||
|
class ZimiFan(ZimiEntity, FanEntity):
|
||||||
|
"""Representation of a Zimi fan."""
|
||||||
|
|
||||||
|
_attr_speed_range = (0, 7)
|
||||||
|
|
||||||
|
_attr_supported_features = (
|
||||||
|
FanEntityFeature.SET_SPEED
|
||||||
|
| FanEntityFeature.TURN_OFF
|
||||||
|
| FanEntityFeature.TURN_ON
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_set_percentage(self, percentage: int) -> None:
|
||||||
|
"""Set the desired speed for the fan."""
|
||||||
|
|
||||||
|
if percentage == 0:
|
||||||
|
await self.async_turn_off()
|
||||||
|
return
|
||||||
|
|
||||||
|
target_speed = math.ceil(
|
||||||
|
percentage_to_ranged_value(self._attr_speed_range, percentage)
|
||||||
|
)
|
||||||
|
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Sending async_set_percentage() for %s with percentage %s",
|
||||||
|
self.name,
|
||||||
|
percentage,
|
||||||
|
)
|
||||||
|
|
||||||
|
await self._device.set_fanspeed(target_speed)
|
||||||
|
|
||||||
|
async def async_turn_on(
|
||||||
|
self,
|
||||||
|
percentage: int | None = None,
|
||||||
|
preset_mode: str | None = None,
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> None:
|
||||||
|
"""Instruct the fan to turn on."""
|
||||||
|
|
||||||
|
_LOGGER.debug("Sending turn_on() for %s", self.name)
|
||||||
|
await self._device.turn_on()
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
|
"""Instruct the fan to turn off."""
|
||||||
|
|
||||||
|
_LOGGER.debug("Sending turn_off() for %s", self.name)
|
||||||
|
|
||||||
|
await self._device.turn_off()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def percentage(self) -> int:
|
||||||
|
"""Return the current speed percentage for the fan."""
|
||||||
|
if not self._device.fanspeed:
|
||||||
|
return 0
|
||||||
|
return ranged_value_to_percentage(self._attr_speed_range, self._device.fanspeed)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def speed_count(self) -> int:
|
||||||
|
"""Return the number of speeds the fan supports."""
|
||||||
|
return int_states_in_range(self._attr_speed_range)
|
Loading…
x
Reference in New Issue
Block a user