Skip to content

Commit

Permalink
Add valve support
Browse files Browse the repository at this point in the history
  • Loading branch information
Friday-The13-rus authored Nov 1, 2024
1 parent 5b2f2f1 commit ca0d5ce
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
29 changes: 29 additions & 0 deletions custom_components/yandex_smart_home/capability_onoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@
script,
switch,
vacuum,
valve,
water_heater,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
MAJOR_VERSION,
MINOR_VERSION,
SERVICE_CLOSE_COVER,
SERVICE_CLOSE_VALVE,
SERVICE_LOCK,
SERVICE_OPEN_COVER,
SERVICE_OPEN_VALVE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
SERVICE_UNLOCK,
STATE_OFF,
STATE_ON,
STATE_OPEN,
)
from homeassistant.core import DOMAIN as HA_DOMAIN, Context
from homeassistant.exceptions import ServiceNotFound
Expand Down Expand Up @@ -504,3 +508,28 @@ def _get_water_heater_operation(self, required_mode: str, operations_list: list[
return operation

return None


@STATE_CAPABILITIES_REGISTRY.register
class OnOffCapabilityValve(OnOffCapability):
"""Capability to open or close a valve."""

@property
def supported(self) -> bool:
"""Test if the capability is supported."""
return bool(self.state.domain == valve.DOMAIN)

def get_value(self) -> bool | None:
"""Return the current capability value."""
return self.state.state == STATE_OPEN

async def _set_instance_state(self, context: Context, state: OnOffCapabilityInstanceActionState) -> None:
"""Change the capability state."""
if state.value:
service = SERVICE_OPEN_VALVE
else:
service = SERVICE_CLOSE_VALVE

await self._hass.services.async_call(
valve.DOMAIN, service, {ATTR_ENTITY_ID: self.state.entity_id}, blocking=True, context=context
)
33 changes: 32 additions & 1 deletion custom_components/yandex_smart_home/capability_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from typing import Any, Protocol

from homeassistant.components import climate, cover, fan, humidifier, light, media_player, water_heater
from homeassistant.components import climate, cover, fan, humidifier, light, media_player, valve, water_heater
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
Expand Down Expand Up @@ -599,3 +599,34 @@ def _default_range(self) -> RangeCapabilityRange:
max=999,
precision=1,
)


@STATE_CAPABILITIES_REGISTRY.register
class ValvePositionCapability(StateRangeCapability):
"""Capability to control position of a device."""

instance = RangeCapabilityInstance.OPEN

@property
def supported(self) -> bool:
"""Test if the capability is supported."""
return self.state.domain == valve.DOMAIN and bool(self._state_features & valve.ValveEntityFeature.SET_POSITION)

@property
def support_random_access(self) -> bool:
"""Test if the capability accept arbitrary values to be set."""
return True

async def set_instance_state(self, context: Context, state: RangeCapabilityInstanceActionState) -> None:
"""Change the capability state."""
await self._hass.services.async_call(
valve.DOMAIN,
valve.SERVICE_SET_VALVE_POSITION,
{ATTR_ENTITY_ID: self.state.entity_id, valve.ATTR_POSITION: self._get_service_call_value(state)},
blocking=True,
context=context,
)

def _get_value(self) -> float | None:
"""Return the current capability value (unguarded)."""
return self._convert_to_float(self.state.attributes.get(valve.ATTR_CURRENT_POSITION))
2 changes: 2 additions & 0 deletions custom_components/yandex_smart_home/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
sensor,
switch,
vacuum,
valve,
water_heater,
)
from homeassistant.const import ATTR_DEVICE_CLASS, CLOUD_NEVER_EXPOSED_ENTITIES, CONF_NAME, STATE_UNAVAILABLE
Expand Down Expand Up @@ -104,6 +105,7 @@
sensor.DOMAIN: DeviceType.SENSOR,
switch.DOMAIN: DeviceType.SWITCH,
vacuum.DOMAIN: DeviceType.VACUUM_CLEANER,
valve.DOMAIN: DeviceType.OPENABLE_VALVE,
water_heater.DOMAIN: DeviceType.KETTLE,
}

Expand Down
1 change: 1 addition & 0 deletions custom_components/yandex_smart_home/schema/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class DeviceType(StrEnum):
MULTICOOKER = "devices.types.cooking.multicooker"
OPENABLE = "devices.types.openable"
OPENABLE_CURTAIN = "devices.types.openable.curtain"
OPENABLE_VALVE = "devices.types.openable.valve"
HUMIDIFIER = "devices.types.humidifier"
PURIFIER = "devices.types.purifier"
VACUUM_CLEANER = "devices.types.vacuum_cleaner"
Expand Down

0 comments on commit ca0d5ce

Please sign in to comment.