Skip to content

Commit

Permalink
Merge pull request #59 from cairy/master
Browse files Browse the repository at this point in the history
添加燃气热水器
  • Loading branch information
banto6 authored Sep 14, 2023
2 parents 4877d53 + f39dd4f commit 099c48f
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
24 changes: 24 additions & 0 deletions custom_components/haier/core/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def parse_global(self, attributes: List[dict]):
if len(list(set(feature_fields) - set(all_attribute_keys))) == 0:
yield self._parse_as_climate(attributes, feature_fields)
break
# 燃气热水器
if 'outWaterTemp' in all_attribute_keys and 'inWaterTemp' in all_attribute_keys and 'gasPressure' in all_attribute_keys:
yield self._parse_as_gas_water_heater(attributes)

@staticmethod
def _parse_as_sensor(attribute):
Expand Down Expand Up @@ -196,3 +199,24 @@ def _parse_as_climate(attributes: List[dict], feature_fields: List[str]):
}

return HaierAttribute('climate', 'Climate', Platform.CLIMATE, options, ext)

@staticmethod
def _parse_as_gas_water_heater(attributes: List[dict]):
for attr in attributes:
if attr['name'] == 'targetTemp':
target_temperature_attr = attr
break
else:
raise RuntimeError('targetTemp attr not found')

options = {
'min_temp': target_temperature_attr['variants']['minValue'],
'max_temp': target_temperature_attr['variants']['maxValue'],
'target_temperature_step': target_temperature_attr['variants']['step']
}

ext = {
'customize': True,
}

return HaierAttribute('gas_water_heater', 'GasWaterHeater', Platform.WATER_HEATER, options, ext)
97 changes: 97 additions & 0 deletions custom_components/haier/water_heater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Support for water heaters."""
import logging

from homeassistant.components.water_heater import (
WaterHeaterEntity,
STATE_GAS,
SUPPORT_AWAY_MODE,
SUPPORT_TARGET_TEMPERATURE,
SUPPORT_OPERATION_MODE,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, STATE_OFF, UnitOfTemperature, TEMP_CELSIUS,Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity import DeviceInfo

from . import async_register_entity
from .coordinator import DeviceCoordinator
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__)

SUPPORT_FLAGS = (
SUPPORT_AWAY_MODE | SUPPORT_TARGET_TEMPERATURE | SUPPORT_OPERATION_MODE
)


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


class HaierWaterHeater(HaierAbstractEntity,WaterHeaterEntity):

def __init__(self, coordinator: DeviceCoordinator, device: HaierDevice, attribute: HaierAttribute):
super().__init__(coordinator, device, attribute)
self._attr_temperature_unit = TEMP_CELSIUS
self._attr_supported_features = SUPPORT_FLAGS
# 默认的0-70温度范围太宽,homekit不支持
self._attr_min_temp = 35
self._attr_max_temp = 50

@property
def operation_list(self):
"""List of available operation modes."""
return [STATE_OFF, STATE_GAS]


def set_temperature(self, **kwargs) -> None:
self._send_command({
'targetTemp': kwargs['temperature']
})

def _update_value(self):
if 'outWaterTemp' in self.coordinator.data:
self._attr_current_temperature = float(self.coordinator.data['outWaterTemp'])

self._attr_target_temperature = float(self.coordinator.data['targetTemp'])

if not try_read_as_bool(self.coordinator.data['onOffStatus']):
# 关机状态
self._attr_current_operation = STATE_OFF
self._attr_is_away_mode_on = True
else:
# 开机状态
self._attr_current_operation = STATE_GAS
self._attr_is_away_mode_on = False

def turn_away_mode_on(self):
"""Turn away mode on."""
self._send_command({
'onOffStatus': False
})

def turn_away_mode_off(self):
"""Turn away mode off."""
self._send_command({
'onOffStatus': True
})

def set_operation_mode(self,operation_mode):
"""Set operation mode"""
if operation_mode == STATE_GAS:
power_state = True
else:
power_state = False
self._send_command({
'onOffStatus': power_state
})

0 comments on commit 099c48f

Please sign in to comment.