Skip to content

Commit

Permalink
add continuous switch according to official app only available when m…
Browse files Browse the repository at this point in the history
…anual mode is on. Fix #48.  (#49)

* fix: Change the name of "Fresh" mode to "Purification" synchronized with the official app

* fix: command sent to wrong device when multiple device available.

* feat: add continuous switch according to official app only available when manual mode is on.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
sususweet and pre-commit-ci[bot] authored Dec 19, 2024
1 parent a7aae81 commit ba234cb
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 17 deletions.
3 changes: 2 additions & 1 deletion custom_components/deye_dehumidifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ async def publish_command_async(self, attribute, value):
"""Push command to a queue and deal with them together."""
self.async_write_ha_state()
self.hass.bus.fire(
"call_humidifier_method", {"prop": attribute, "value": value}
"call_humidifier_method",
{"device_id": self._device["device_id"], "prop": attribute, "value": value},
)
await self.coordinator.async_request_refresh()

Expand Down
5 changes: 1 addition & 4 deletions custom_components/deye_dehumidifier/data_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ async def poll_device_state(self) -> DeyeDeviceState:
self._device["device_id"],
QUERY_DEVICE_STATE_COMMAND,
)
response = await asyncio.wait_for(
self.receive_queue.get(), timeout=10
) # 设置超时时间
# _LOGGER.error(response.to_command().json())
response = await asyncio.wait_for(self.receive_queue.get(), timeout=10)
return response
elif self._device["platform"] == 2:
response = DeyeDeviceState(
Expand Down
15 changes: 7 additions & 8 deletions custom_components/deye_dehumidifier/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ async def async_setup_entry(
)
async_add_entities([deye_dehumidifier])

async def call_method(event):
prop = event.data.get("prop")
value = event.data.get("value")
await deye_dehumidifier.publish_command(prop, value)

hass.bus.async_listen("call_humidifier_method", call_method)


class DeyeDehumidifier(DeyeEntity, HumidifierEntity):
"""Dehumidifier entity."""
Expand Down Expand Up @@ -80,15 +73,21 @@ def __init__(
self._attr_entity_picture = device["product_icon"]
self.data_change_list: dict = dict()

async def call_method(self, event):
if event.data.get("device_id") == self._device["device_id"]:
prop = event.data.get("prop")
value = event.data.get("value")
await self.publish_command(prop, value)

async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()
self.hass.helpers.event.async_track_time_interval(
self.put_device_state, timedelta(seconds=5)
)
self.hass.bus.async_listen("call_humidifier_method", self.call_method)

@callback
async def put_device_state(self, now: datetime | None = None) -> None:
# _LOGGER.error(self.data_change_list)
if len(self.data_change_list.items()) > 0:
command = self.device_state.to_command()
for prop, value in self.data_change_list.items():
Expand Down
6 changes: 6 additions & 0 deletions custom_components/deye_dehumidifier/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
}
},
"switch": {
"continuous": {
"default": "mdi:autorenew",
"state": {
"off": "mdi:autorenew-off"
}
},
"child_lock": {
"default": "mdi:baby-carriage",
"state": {
Expand Down
2 changes: 1 addition & 1 deletion custom_components/deye_dehumidifier/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"iot_class": "cloud_push",
"issue_tracker": "https://github.com/stackia/ha-deye-dehumidifier/issues",
"requirements": ["libdeye==1.3.0"],
"version": "1.6.0"
"version": "1.6.2"
}
46 changes: 45 additions & 1 deletion custom_components/deye_dehumidifier/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from libdeye.cloud_api import DeyeCloudApi
from libdeye.mqtt_client import DeyeMqttClient
from libdeye.types import DeyeApiResponseDeviceInfo
from libdeye.types import DeyeApiResponseDeviceInfo, DeyeDeviceMode
from libdeye.utils import get_product_feature_config

from . import DeyeEntity
Expand All @@ -30,6 +30,9 @@ async def async_setup_entry(
async_add_entities(
[DeyeChildLockSwitch(device, data[DATA_MQTT_CLIENT], data[DATA_CLOUD_API])]
)
async_add_entities(
[DeyeContinuousSwitch(device, data[DATA_MQTT_CLIENT], data[DATA_CLOUD_API])]
)
feature_config = get_product_feature_config(device["product_id"])
if feature_config["anion"]:
async_add_entities(
Expand Down Expand Up @@ -148,3 +151,44 @@ async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the water pump off."""
self.device_state.water_pump_switch = False
await self.publish_command_async("water_pump_switch", False)


class DeyeContinuousSwitch(DeyeEntity, SwitchEntity):
"""Continuous switch entity."""

_attr_translation_key = "continuous"
_attr_device_class = SwitchDeviceClass.SWITCH
_attr_entity_category = EntityCategory.CONFIG

def __init__(
self,
device: DeyeApiResponseDeviceInfo,
mqtt_client: DeyeMqttClient,
cloud_api: DeyeCloudApi,
) -> None:
"""Initialize the switch."""
super().__init__(device, mqtt_client, cloud_api)
assert self._attr_unique_id is not None
self._attr_unique_id += "-continuous"
self.entity_id = f"switch.{self.entity_id_base}_continuous"

@property
def available(self):
return (
super().available and self.device_state.mode == DeyeDeviceMode.MANUAL_MODE
)

@property
def is_on(self) -> bool:
"""Return True if the continuous switch is on."""
return self.device_state.target_humidity == 25

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the continuous switch on."""
self.device_state.target_humidity = 25
await self.publish_command_async("target_humidity", 25)

async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the continuous switch off."""
self.device_state.target_humidity = 50
await self.publish_command_async("target_humidity", 50)
3 changes: 3 additions & 0 deletions custom_components/deye_dehumidifier/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
}
},
"switch": {
"continuous": {
"name": "Continuous Dehumidify"
},
"child_lock": {
"name": "Child Lock"
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/deye_dehumidifier/translations/pt-PT.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
}
},
"switch": {
"continuous": {
"name": "Desumidificação contínua"
},
"child_lock": {
"name": "Bloqueio anti crianças"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"mode": {
"state": {
"manual": "手动",
"air_purifier": "清新",
"air_purifier": "净化",
"clothes_dryer": "干衣"
}
}
Expand All @@ -50,6 +50,9 @@
}
},
"switch": {
"continuous": {
"name": "连续除湿"
},
"child_lock": {
"name": "童锁"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"mode": {
"state": {
"manual": "手動",
"air_purifier": "清新",
"air_purifier": "淨化",
"clothes_dryer": "乾衣"
}
}
Expand All @@ -50,6 +50,9 @@
}
},
"switch": {
"continuous": {
"name": "連續除濕"
},
"child_lock": {
"name": "童鎖"
},
Expand Down

0 comments on commit ba234cb

Please sign in to comment.