Skip to content

Commit

Permalink
Implement datetime time entity (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesserockz committed Mar 20, 2024
1 parent ff23e4c commit 7da8a35
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 53 deletions.
42 changes: 42 additions & 0 deletions aioesphomeapi/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ service APIConnection {
rpc lock_command (LockCommandRequest) returns (void) {}
rpc media_player_command (MediaPlayerCommandRequest) returns (void) {}
rpc date_command (DateCommandRequest) returns (void) {}
rpc time_command (TimeCommandRequest) returns (void) {}

rpc subscribe_bluetooth_le_advertisements (SubscribeBluetoothLEAdvertisementsRequest) returns (void) {}
rpc bluetooth_device_request(BluetoothDeviceRequest) returns (void) {}
Expand Down Expand Up @@ -1660,3 +1661,44 @@ message DateCommandRequest {
uint32 month = 3;
uint32 day = 4;
}

// ==================== DATETIME TIME ====================
message ListEntitiesTimeResponse {
option (id) = 103;
option (source) = SOURCE_SERVER;
option (ifdef) = "USE_DATETIME_TIME";

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;
}
message TimeStateResponse {
option (id) = 104;
option (source) = SOURCE_SERVER;
option (ifdef) = "USE_DATETIME_TIME";
option (no_delay) = true;

fixed32 key = 1;
// If the time does not have a valid state yet.
// Equivalent to `!obj->has_state()` - inverse logic to make state packets smaller
bool missing_state = 2;
uint32 hour = 3;
uint32 minute = 4;
uint32 second = 5;
}
message TimeCommandRequest {
option (id) = 105;
option (source) = SOURCE_CLIENT;
option (ifdef) = "USE_DATETIME_TIME";
option (no_delay) = true;

fixed32 key = 1;
uint32 hour = 2;
uint32 minute = 3;
uint32 second = 4;
}
142 changes: 89 additions & 53 deletions aioesphomeapi/api_pb2.py

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions aioesphomeapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
SubscribeVoiceAssistantRequest,
SwitchCommandRequest,
TextCommandRequest,
TimeCommandRequest,
UnsubscribeBluetoothLEAdvertisementsRequest,
VoiceAssistantEventData,
VoiceAssistantEventResponse,
Expand Down Expand Up @@ -1108,6 +1109,11 @@ def date_command(self, key: int, year: int, month: int, day: int) -> None:
DateCommandRequest(key=key, year=year, month=month, day=day)
)

def time_command(self, key: int, hour: int, minute: int, second: int) -> None:
self._get_connection().send_message(
TimeCommandRequest(key=key, hour=hour, minute=minute, second=second)
)

def select_command(self, key: int, state: str) -> None:
self._get_connection().send_message(SelectCommandRequest(key=key, state=state))

Expand Down
6 changes: 6 additions & 0 deletions aioesphomeapi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
ListEntitiesSwitchResponse,
ListEntitiesTextResponse,
ListEntitiesTextSensorResponse,
ListEntitiesTimeResponse,
LockCommandRequest,
LockStateResponse,
MediaPlayerCommandRequest,
Expand Down Expand Up @@ -103,6 +104,8 @@
TextCommandRequest,
TextSensorStateResponse,
TextStateResponse,
TimeCommandRequest,
TimeStateResponse,
UnsubscribeBluetoothLEAdvertisementsRequest,
VoiceAssistantEventResponse,
VoiceAssistantRequest,
Expand Down Expand Up @@ -360,4 +363,7 @@ def __init__(self, error: BluetoothGATTError) -> None:
100: ListEntitiesDateResponse,
101: DateStateResponse,
102: DateCommandRequest,
103: ListEntitiesTimeResponse,
104: TimeStateResponse,
105: TimeCommandRequest,
}
18 changes: 18 additions & 0 deletions aioesphomeapi/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,22 @@ class DateState(EntityState):
day: int = 0


# ==================== DATETIME TIME ====================


@_frozen_dataclass_decorator
class TimeInfo(EntityInfo):
pass


@_frozen_dataclass_decorator
class TimeState(EntityState):
missing_state: bool = False
hour: int = 0
minute: int = 0
second: int = 0


# ==================== SELECT ====================
@_frozen_dataclass_decorator
class SelectInfo(EntityInfo):
Expand Down Expand Up @@ -830,6 +846,7 @@ class TextState(EntityState):
"media_player": MediaPlayerInfo,
"alarm_control_panel": AlarmControlPanelInfo,
"text": TextInfo,
"time": TimeInfo,
}


Expand Down Expand Up @@ -1183,6 +1200,7 @@ class VoiceAssistantEventType(APIIntEnum):
MediaPlayerInfo: "media_player",
AlarmControlPanelInfo: "alarm_control_panel",
TextInfo: "text_info",
TimeInfo: "time",
}


Expand Down
6 changes: 6 additions & 0 deletions aioesphomeapi/model_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ListEntitiesSwitchResponse,
ListEntitiesTextResponse,
ListEntitiesTextSensorResponse,
ListEntitiesTimeResponse,
LockStateResponse,
MediaPlayerStateResponse,
NumberStateResponse,
Expand All @@ -38,6 +39,7 @@
SwitchStateResponse,
TextSensorStateResponse,
TextStateResponse,
TimeStateResponse,
)
from .model import (
AlarmControlPanelEntityState,
Expand Down Expand Up @@ -76,6 +78,8 @@
TextSensorInfo,
TextSensorState,
TextState,
TimeInfo,
TimeState,
)

SUBSCRIBE_STATES_RESPONSE_TYPES: dict[Any, type[EntityState]] = {
Expand All @@ -95,6 +99,7 @@
LockStateResponse: LockEntityState,
MediaPlayerStateResponse: MediaPlayerEntityState,
AlarmControlPanelStateResponse: AlarmControlPanelEntityState,
TimeStateResponse: TimeState,
}

LIST_ENTITIES_SERVICES_RESPONSE_TYPES: dict[Any, type[EntityInfo] | None] = {
Expand All @@ -117,4 +122,5 @@
ListEntitiesLockResponse: LockInfo,
ListEntitiesMediaPlayerResponse: MediaPlayerInfo,
ListEntitiesAlarmControlPanelResponse: AlarmControlPanelInfo,
ListEntitiesTimeResponse: TimeInfo,
}
25 changes: 25 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
SubscribeVoiceAssistantRequest,
SwitchCommandRequest,
TextCommandRequest,
TimeCommandRequest,
VoiceAssistantAudioSettings,
VoiceAssistantEventData,
VoiceAssistantEventResponse,
Expand Down Expand Up @@ -641,6 +642,30 @@ async def test_date_command(
send.assert_called_once_with(DateCommandRequest(**req))


# Test time command
@pytest.mark.asyncio
@pytest.mark.parametrize(
"cmd, req",
[
(
dict(key=1, hour=12, minute=30, second=30),
dict(key=1, hour=12, minute=30, second=30),
),
(
dict(key=1, hour=0, minute=0, second=0),
dict(key=1, hour=0, minute=0, second=0),
),
],
)
async def test_time_command(
auth_client: APIClient, cmd: dict[str, Any], req: dict[str, Any]
) -> None:
send = patch_send(auth_client)

auth_client.time_command(**cmd)
send.assert_called_once_with(TimeCommandRequest(**req))


@pytest.mark.asyncio
@pytest.mark.parametrize(
"cmd, req",
Expand Down
7 changes: 7 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ListEntitiesServicesResponse,
ListEntitiesSwitchResponse,
ListEntitiesTextSensorResponse,
ListEntitiesTimeResponse,
LockStateResponse,
MediaPlayerStateResponse,
NumberStateResponse,
Expand All @@ -45,6 +46,7 @@
SwitchStateResponse,
TextSensorStateResponse,
TextStateResponse,
TimeStateResponse,
)
from aioesphomeapi.model import (
_TYPE_TO_NAME,
Expand Down Expand Up @@ -98,6 +100,8 @@
TextSensorInfo,
TextSensorState,
TextState,
TimeInfo,
TimeState,
UserService,
UserServiceArg,
UserServiceArgType,
Expand Down Expand Up @@ -261,6 +265,8 @@ def test_api_version_ord():
(AlarmControlPanelInfo, ListEntitiesAlarmControlPanelResponse),
(AlarmControlPanelEntityState, AlarmControlPanelStateResponse),
(TextState, TextStateResponse),
(TimeInfo, ListEntitiesTimeResponse),
(TimeState, TimeStateResponse),
],
)
def test_basic_pb_conversions(model, pb):
Expand Down Expand Up @@ -376,6 +382,7 @@ def test_user_service_conversion():
MediaPlayerInfo,
AlarmControlPanelInfo,
TextInfo,
TimeInfo,
],
)
def test_build_unique_id(model):
Expand Down

0 comments on commit 7da8a35

Please sign in to comment.