Skip to content

Commit

Permalink
Merge pull request #159 from qtip09/master
Browse files Browse the repository at this point in the history
增加窗帘cover实体,支持窗帘打开、关闭、暂停和设置开合度
  • Loading branch information
banto6 authored Jun 25, 2024
2 parents be5e352 + dcfa810 commit cfe8977
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
3 changes: 2 additions & 1 deletion custom_components/haier/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
Platform.BINARY_SENSOR,
Platform.SWITCH,
Platform.CLIMATE,
Platform.WATER_HEATER
Platform.WATER_HEATER,
Platform.COVER
]

FILTER_TYPE_INCLUDE = 'include'
Expand Down
21 changes: 21 additions & 0 deletions custom_components/haier/core/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def parse_global(self, attributes: List[dict]):
if 'outWaterTemp' in all_attribute_keys and 'targetTemp' in all_attribute_keys and 'totalUseGasL' in all_attribute_keys:
yield self._parse_as_gas_water_heater(attributes)

# 窗帘
if 'openDegree' in all_attribute_keys:
yield self._parse_as_cover(attributes)



@staticmethod
def _parse_as_sensor(attribute):
if V1SpecAttributeParser._is_binary_attribute(attribute):
Expand Down Expand Up @@ -198,6 +204,21 @@ def _parse_as_gas_water_heater(attributes: List[dict]):

return HaierAttribute('gas_water_heater', 'GasWaterHeater', Platform.WATER_HEATER, options, ext)

@staticmethod
def _parse_as_cover(attributes: List[dict]):
for attr in attributes:
if attr["name"] == "openDegree":
step = attr['valueRange']['dataStep']
options = {
'native_min_value': float(step['minValue']),
'native_max_value': float(step['maxValue']),
'native_step': step['step']
}
ext = {
'customize': True,
}
return HaierAttribute('cover', 'Cover', Platform.COVER, options,ext)

@staticmethod
def _is_binary_attribute(attribute):
valueRange = attribute['valueRange']
Expand Down
53 changes: 53 additions & 0 deletions custom_components/haier/cover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import logging
from homeassistant.components.cover import CoverEntity,CoverEntityFeature;
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.const import UnitOfTemperature, Platform
from . import async_register_entity
from .core.attribute import HaierAttribute
from .core.device import HaierDevice
from .entity import HaierAbstractEntity
from .helpers import try_read_as_bool

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities) -> None:
await async_register_entity(
hass,
entry,
async_add_entities,
Platform.COVER,
lambda device, attribute: HaierCover(device, attribute)
)

class HaierCover(HaierAbstractEntity, CoverEntity):
def __init__(self, device: HaierDevice, attribute: HaierAttribute):
super().__init__(device, attribute)

def _update_value(self):
self._attr_is_closed = try_read_as_bool(self._attributes_data['onOffStatus'])
self._attr_current_cover_position = int(self._attributes_data['openDegree'])

def open_cover(self, **kwargs) -> None:
_LOGGER.debug("执行窗帘打开")
self._send_command({
'onOffStatus': True
})

def close_cover(self, **kwargs) -> None:
_LOGGER.debug("执行窗帘关闭")
self._send_command({
'onOffStatus': False
})

def stop_cover(self, **kwargs) -> None:
_LOGGER.debug("执行窗帘暂停")
self._send_command({
'pause': True
})

def set_cover_position(self,position: int) -> None:
_LOGGER.debug("执行设置窗帘开合度")
self._send_command({
'openDegree': position
})

0 comments on commit cfe8977

Please sign in to comment.