3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
8 from yolink.client_request
import ClientRequest
9 from yolink.const
import ATTR_DEVICE_WATER_METER_CONTROLLER
10 from yolink.device
import YoLinkDevice
15 ValveEntityDescription,
22 from .const
import DEV_MODEL_WATER_METER_YS5007, DOMAIN
23 from .coordinator
import YoLinkCoordinator
24 from .entity
import YoLinkEntity
27 @dataclass(frozen=True)
29 """YoLink ValveEntityDescription."""
31 exists_fn: Callable[[YoLinkDevice], bool] =
lambda _:
True
32 value: Callable =
lambda state: state
35 DEVICE_TYPES: tuple[YoLinkValveEntityDescription, ...] = (
38 translation_key=
"meter_valve_state",
39 device_class=ValveDeviceClass.WATER,
40 value=
lambda value: value !=
"open" if value
is not None else None,
41 exists_fn=
lambda device: device.device_type
42 == ATTR_DEVICE_WATER_METER_CONTROLLER
43 and not device.device_model_name.startswith(DEV_MODEL_WATER_METER_YS5007),
47 DEVICE_TYPE = [ATTR_DEVICE_WATER_METER_CONTROLLER]
52 config_entry: ConfigEntry,
53 async_add_entities: AddEntitiesCallback,
55 """Set up YoLink valve from a config entry."""
56 device_coordinators = hass.data[DOMAIN][config_entry.entry_id].device_coordinators
57 valve_device_coordinators = [
59 for device_coordinator
in device_coordinators.values()
60 if device_coordinator.device.device_type
in DEVICE_TYPE
64 for valve_device_coordinator
in valve_device_coordinators
65 for description
in DEVICE_TYPES
66 if description.exists_fn(valve_device_coordinator.device)
71 """YoLink Valve Entity."""
73 entity_description: YoLinkValveEntityDescription
77 config_entry: ConfigEntry,
78 coordinator: YoLinkCoordinator,
79 description: YoLinkValveEntityDescription,
81 """Init YoLink valve."""
82 super().
__init__(config_entry, coordinator)
84 ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE
88 f
"{coordinator.device.device_id} {self.entity_description.key}"
93 """Update HA Entity State."""
104 """Call setState api to change valve state."""
105 await self.
call_devicecall_device(ClientRequest(
"setState", {
"valve": state}))
110 """Open the valve."""
None call_device(self, ClientRequest request)
None _async_invoke_device(self, str state)
None __init__(self, ConfigEntry config_entry, YoLinkCoordinator coordinator, YoLinkValveEntityDescription description)
None async_open_valve(self)
None update_entity_state(self, dict[str, str|list[str]] state)
None async_close_valve(self)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)