Add valve component (#852)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Keith Burzinski 2024-04-16 20:45:33 -05:00 committed by GitHub
parent b10cbd6c9a
commit 0ed69bbb30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 254 additions and 55 deletions

View File

@ -44,6 +44,7 @@ service APIConnection {
rpc siren_command (SirenCommandRequest) returns (void) {}
rpc button_command (ButtonCommandRequest) returns (void) {}
rpc lock_command (LockCommandRequest) returns (void) {}
rpc valve_command (ValveCommandRequest) returns (void) {}
rpc media_player_command (MediaPlayerCommandRequest) returns (void) {}
rpc date_command (DateCommandRequest) returns (void) {}
rpc time_command (TimeCommandRequest) returns (void) {}
@ -1718,3 +1719,52 @@ message TimeCommandRequest {
uint32 minute = 3;
uint32 second = 4;
}
// ==================== VALVE ====================
message ListEntitiesValveResponse {
option (id) = 109;
option (source) = SOURCE_SERVER;
option (ifdef) = "USE_VALVE";
string object_id = 1;
fixed32 key = 2;
string name = 3;
string unique_id = 4;
string icon = 5;
bool disabled_by_default = 6;
EntityCategory entity_category = 7;
string device_class = 8;
bool assumed_state = 9;
bool supports_position = 10;
bool supports_stop = 11;
}
enum ValveOperation {
VALVE_OPERATION_IDLE = 0;
VALVE_OPERATION_IS_OPENING = 1;
VALVE_OPERATION_IS_CLOSING = 2;
}
message ValveStateResponse {
option (id) = 110;
option (source) = SOURCE_SERVER;
option (ifdef) = "USE_VALVE";
option (no_delay) = true;
fixed32 key = 1;
float position = 2;
ValveOperation current_operation = 3;
}
message ValveCommandRequest {
option (id) = 111;
option (source) = SOURCE_CLIENT;
option (ifdef) = "USE_VALVE";
option (no_delay) = true;
fixed32 key = 1;
bool has_position = 2;
float position = 3;
bool stop = 4;
}

File diff suppressed because one or more lines are too long

View File

@ -67,6 +67,7 @@ from .api_pb2 import ( # type: ignore
TextCommandRequest,
TimeCommandRequest,
UnsubscribeBluetoothLEAdvertisementsRequest,
ValveCommandRequest,
VoiceAssistantAudio,
VoiceAssistantEventData,
VoiceAssistantEventResponse,
@ -1157,6 +1158,20 @@ class APIClient:
req.code = code
self._get_connection().send_message(req)
def valve_command(
self,
key: int,
position: float | None = None,
stop: bool = False,
) -> None:
req = ValveCommandRequest(key=key)
if position is not None:
req.has_position = True
req.position = position
if stop:
req.stop = stop
self._get_connection().send_message(req)
def media_player_command(
self,
key: int,

View File

@ -77,6 +77,7 @@ from .api_pb2 import ( # type: ignore
ListEntitiesTextResponse,
ListEntitiesTextSensorResponse,
ListEntitiesTimeResponse,
ListEntitiesValveResponse,
LockCommandRequest,
LockStateResponse,
MediaPlayerCommandRequest,
@ -107,6 +108,8 @@ from .api_pb2 import ( # type: ignore
TimeCommandRequest,
TimeStateResponse,
UnsubscribeBluetoothLEAdvertisementsRequest,
ValveCommandRequest,
ValveStateResponse,
VoiceAssistantAudio,
VoiceAssistantEventResponse,
VoiceAssistantRequest,
@ -368,4 +371,7 @@ MESSAGE_TYPE_TO_PROTO = {
104: TimeStateResponse,
105: TimeCommandRequest,
106: VoiceAssistantAudio,
109: ListEntitiesValveResponse,
110: ValveStateResponse,
111: ValveCommandRequest,
}

View File

@ -753,6 +753,31 @@ class LockEntityState(EntityState):
)
# ==================== VALVE ====================
@_frozen_dataclass_decorator
class ValveInfo(EntityInfo):
device_class: str = ""
assumed_state: bool = False
supports_stop: bool = False
supports_position: bool = False
class ValveOperation(APIIntEnum):
IDLE = 0
IS_OPENING = 1
IS_CLOSING = 2
@_frozen_dataclass_decorator
class ValveState(EntityState):
position: float = converter_field(
default=0.0, converter=fix_float_single_double_conversion
)
current_operation: ValveOperation | None = converter_field(
default=ValveOperation.IDLE, converter=ValveOperation.convert
)
# ==================== MEDIA PLAYER ====================
class MediaPlayerState(APIIntEnum):
NONE = 0
@ -868,6 +893,7 @@ COMPONENT_TYPE_TO_INFO: dict[str, type[EntityInfo]] = {
"alarm_control_panel": AlarmControlPanelInfo,
"text": TextInfo,
"time": TimeInfo,
"valve": ValveInfo,
}
@ -1228,6 +1254,7 @@ _TYPE_TO_NAME = {
AlarmControlPanelInfo: "alarm_control_panel",
TextInfo: "text_info",
TimeInfo: "time",
ValveInfo: "valve",
}

View File

@ -30,6 +30,7 @@ from .api_pb2 import ( # type: ignore
ListEntitiesTextResponse,
ListEntitiesTextSensorResponse,
ListEntitiesTimeResponse,
ListEntitiesValveResponse,
LockStateResponse,
MediaPlayerStateResponse,
NumberStateResponse,
@ -40,6 +41,7 @@ from .api_pb2 import ( # type: ignore
TextSensorStateResponse,
TextStateResponse,
TimeStateResponse,
ValveStateResponse,
)
from .model import (
AlarmControlPanelEntityState,
@ -80,6 +82,8 @@ from .model import (
TextState,
TimeInfo,
TimeState,
ValveInfo,
ValveState,
)
SUBSCRIBE_STATES_RESPONSE_TYPES: dict[Any, type[EntityState]] = {
@ -100,6 +104,7 @@ SUBSCRIBE_STATES_RESPONSE_TYPES: dict[Any, type[EntityState]] = {
MediaPlayerStateResponse: MediaPlayerEntityState,
AlarmControlPanelStateResponse: AlarmControlPanelEntityState,
TimeStateResponse: TimeState,
ValveStateResponse: ValveState,
}
LIST_ENTITIES_SERVICES_RESPONSE_TYPES: dict[Any, type[EntityInfo] | None] = {
@ -123,4 +128,5 @@ LIST_ENTITIES_SERVICES_RESPONSE_TYPES: dict[Any, type[EntityInfo] | None] = {
ListEntitiesMediaPlayerResponse: MediaPlayerInfo,
ListEntitiesAlarmControlPanelResponse: AlarmControlPanelInfo,
ListEntitiesTimeResponse: TimeInfo,
ListEntitiesValveResponse: ValveInfo,
}

View File

@ -64,6 +64,7 @@ from aioesphomeapi.api_pb2 import (
SwitchCommandRequest,
TextCommandRequest,
TimeCommandRequest,
ValveCommandRequest,
VoiceAssistantAudio,
VoiceAssistantAudioSettings,
VoiceAssistantEventData,
@ -692,6 +693,49 @@ async def test_lock_command(
send.assert_called_once_with(LockCommandRequest(**req))
@pytest.mark.asyncio
@pytest.mark.parametrize(
"cmd, req",
[
(dict(key=1), dict(key=1)),
(dict(key=1, position=1.0),),
(dict(key=1, position=0.0),),
(dict(key=1, stop=True),),
],
)
async def test_valve_command(
auth_client: APIClient, cmd: dict[str, Any], req: dict[str, Any]
) -> None:
send = patch_send(auth_client)
auth_client.valve_command(**cmd)
send.assert_called_once_with(ValveCommandRequest(**req))
@pytest.mark.asyncio
@pytest.mark.parametrize(
"cmd, req",
[
(dict(key=1), dict(key=1)),
(dict(key=1, position=0.5), dict(key=1, has_position=True, position=0.5)),
(dict(key=1, position=0.0), dict(key=1, has_position=True, position=0.0)),
(dict(key=1, stop=True), dict(key=1, stop=True)),
(
dict(key=1, position=1.0),
dict(key=1, has_position=True, position=1.0),
),
],
)
async def test_valve_command(
auth_client: APIClient, cmd: dict[str, Any], req: dict[str, Any]
) -> None:
send = patch_send(auth_client)
patch_api_version(auth_client, APIVersion(1, 1))
auth_client.valve_command(**cmd)
send.assert_called_once_with(ValveCommandRequest(**req))
@pytest.mark.asyncio
@pytest.mark.parametrize(
"cmd, req",
@ -2443,6 +2487,7 @@ async def test_calls_after_connection_closed(
client.cover_command,
client.fan_command,
client.light_command,
client.valve_command,
client.media_player_command,
client.siren_command,
):

View File

@ -37,6 +37,7 @@ from aioesphomeapi.api_pb2 import (
ListEntitiesSwitchResponse,
ListEntitiesTextSensorResponse,
ListEntitiesTimeResponse,
ListEntitiesValveResponse,
LockStateResponse,
MediaPlayerStateResponse,
NumberStateResponse,
@ -47,6 +48,7 @@ from aioesphomeapi.api_pb2 import (
TextSensorStateResponse,
TextStateResponse,
TimeStateResponse,
ValveStateResponse,
)
from aioesphomeapi.model import (
_TYPE_TO_NAME,
@ -105,6 +107,8 @@ from aioesphomeapi.model import (
UserService,
UserServiceArg,
UserServiceArgType,
ValveInfo,
ValveState,
VoiceAssistantFeature,
build_unique_id,
converter_field,
@ -261,6 +265,8 @@ def test_api_version_ord():
(ButtonInfo, ListEntitiesButtonResponse),
(LockInfo, ListEntitiesLockResponse),
(LockEntityState, LockStateResponse),
(ValveInfo, ListEntitiesValveResponse),
(ValveState, ValveStateResponse),
(MediaPlayerInfo, ListEntitiesMediaPlayerResponse),
(MediaPlayerEntityState, MediaPlayerStateResponse),
(AlarmControlPanelInfo, ListEntitiesAlarmControlPanelResponse),
@ -380,6 +386,7 @@ def test_user_service_conversion():
CameraInfo,
ClimateInfo,
LockInfo,
ValveInfo,
MediaPlayerInfo,
AlarmControlPanelInfo,
TextInfo,