Simplify similar code for building entity commands (#765)

This commit is contained in:
J. Nick Koston 2023-11-28 06:44:24 -06:00 committed by GitHub
parent 681fec9db9
commit 29faf6746f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 51 deletions

View File

@ -920,8 +920,7 @@ class APIClient:
tilt: float | None = None, tilt: float | None = None,
stop: bool = False, stop: bool = False,
) -> None: ) -> None:
req = CoverCommandRequest() req = CoverCommandRequest(key=key)
req.key = key
apiv = cast(APIVersion, self.api_version) apiv = cast(APIVersion, self.api_version)
if apiv >= APIVersion(1, 1): if apiv >= APIVersion(1, 1):
if position is not None: if position is not None:
@ -954,8 +953,7 @@ class APIClient:
oscillating: bool | None = None, oscillating: bool | None = None,
direction: FanDirection | None = None, direction: FanDirection | None = None,
) -> None: ) -> None:
req = FanCommandRequest() req = FanCommandRequest(key=key)
req.key = key
if state is not None: if state is not None:
req.has_state = True req.has_state = True
req.state = state req.state = state
@ -990,8 +988,7 @@ class APIClient:
flash_length: float | None = None, flash_length: float | None = None,
effect: str | None = None, effect: str | None = None,
) -> None: ) -> None:
req = LightCommandRequest() req = LightCommandRequest(key=key)
req.key = key
if state is not None: if state is not None:
req.has_state = True req.has_state = True
req.state = state req.state = state
@ -1034,11 +1031,7 @@ class APIClient:
self._get_connection().send_message(req) self._get_connection().send_message(req)
async def switch_command(self, key: int, state: bool) -> None: async def switch_command(self, key: int, state: bool) -> None:
req = SwitchCommandRequest() self._get_connection().send_message(SwitchCommandRequest(key=key, state=state))
req.key = key
req.state = state
self._get_connection().send_message(req)
async def climate_command( async def climate_command(
self, self,
@ -1053,8 +1046,7 @@ class APIClient:
preset: ClimatePreset | None = None, preset: ClimatePreset | None = None,
custom_preset: str | None = None, custom_preset: str | None = None,
) -> None: ) -> None:
req = ClimateCommandRequest() req = ClimateCommandRequest(key=key)
req.key = key
if mode is not None: if mode is not None:
req.has_mode = True req.has_mode = True
req.mode = mode req.mode = mode
@ -1091,18 +1083,10 @@ class APIClient:
self._get_connection().send_message(req) self._get_connection().send_message(req)
async def number_command(self, key: int, state: float) -> None: async def number_command(self, key: int, state: float) -> None:
req = NumberCommandRequest() self._get_connection().send_message(NumberCommandRequest(key=key, state=state))
req.key = key
req.state = state
self._get_connection().send_message(req)
async def select_command(self, key: int, state: str) -> None: async def select_command(self, key: int, state: str) -> None:
req = SelectCommandRequest() self._get_connection().send_message(SelectCommandRequest(key=key, state=state))
req.key = key
req.state = state
self._get_connection().send_message(req)
async def siren_command( async def siren_command(
self, self,
@ -1112,8 +1096,7 @@ class APIClient:
volume: float | None = None, volume: float | None = None,
duration: int | None = None, duration: int | None = None,
) -> None: ) -> None:
req = SirenCommandRequest() req = SirenCommandRequest(key=key)
req.key = key
if state is not None: if state is not None:
req.state = state req.state = state
req.has_state = True req.has_state = True
@ -1130,10 +1113,7 @@ class APIClient:
self._get_connection().send_message(req) self._get_connection().send_message(req)
async def button_command(self, key: int) -> None: async def button_command(self, key: int) -> None:
req = ButtonCommandRequest() self._get_connection().send_message(ButtonCommandRequest(key=key))
req.key = key
self._get_connection().send_message(req)
async def lock_command( async def lock_command(
self, self,
@ -1141,12 +1121,9 @@ class APIClient:
command: LockCommand, command: LockCommand,
code: str | None = None, code: str | None = None,
) -> None: ) -> None:
req = LockCommandRequest() req = LockCommandRequest(key=key, command=command)
req.key = key
req.command = command
if code is not None: if code is not None:
req.code = code req.code = code
self._get_connection().send_message(req) self._get_connection().send_message(req)
async def media_player_command( async def media_player_command(
@ -1157,8 +1134,7 @@ class APIClient:
volume: float | None = None, volume: float | None = None,
media_url: str | None = None, media_url: str | None = None,
) -> None: ) -> None:
req = MediaPlayerCommandRequest() req = MediaPlayerCommandRequest(key=key)
req.key = key
if command is not None: if command is not None:
req.command = command req.command = command
req.has_command = True req.has_command = True
@ -1172,17 +1148,12 @@ class APIClient:
self._get_connection().send_message(req) self._get_connection().send_message(req)
async def text_command(self, key: int, state: str) -> None: async def text_command(self, key: int, state: str) -> None:
req = TextCommandRequest() self._get_connection().send_message(TextCommandRequest(key=key, state=state))
req.key = key
req.state = state
self._get_connection().send_message(req)
async def execute_service( async def execute_service(
self, service: UserService, data: ExecuteServiceDataType self, service: UserService, data: ExecuteServiceDataType
) -> None: ) -> None:
req = ExecuteServiceRequest() req = ExecuteServiceRequest(key=service.key)
req.key = service.key
args = [] args = []
for arg_desc in service.args: for arg_desc in service.args:
arg = ExecuteServiceArgument() arg = ExecuteServiceArgument()
@ -1217,11 +1188,9 @@ class APIClient:
async def _request_image( async def _request_image(
self, *, single: bool = False, stream: bool = False self, *, single: bool = False, stream: bool = False
) -> None: ) -> None:
req = CameraImageRequest() self._get_connection().send_message(
req.single = single CameraImageRequest(single=single, stream=stream)
req.stream = stream )
self._get_connection().send_message(req)
async def request_single_image(self) -> None: async def request_single_image(self) -> None:
await self._request_image(single=True) await self._request_image(single=True)
@ -1323,10 +1292,7 @@ class APIClient:
command: AlarmControlPanelCommand, command: AlarmControlPanelCommand,
code: str | None = None, code: str | None = None,
) -> None: ) -> None:
req = AlarmControlPanelCommandRequest() req = AlarmControlPanelCommandRequest(key=key, command=command)
req.key = key
req.command = command
if code is not None: if code is not None:
req.code = code req.code = code
self._get_connection().send_message(req) self._get_connection().send_message(req)