Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Поддержка кранов #549

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,18 +21,22 @@
script,
switch,
vacuum,
valve,
water_heater,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
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 @@ -499,3 +503,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