Skip to content
Open
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
6 changes: 5 additions & 1 deletion htd_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async def async_get_client(
serial_address: str = None,
network_address: Tuple[str, int] = None,
loop: asyncio.AbstractEventLoop = None,
retry_attempts: int = HtdConstants.DEFAULT_RETRY_ATTEMPTS,
) -> BaseClient:
"""
Create a new client object.
Expand All @@ -36,6 +37,7 @@ async def async_get_client(
network_address (str): The address to communicate with over TCP.
serial_address (str): The location of the serial port.
loop (asyncio.AbstractEventLoop): The event loop to use.
retry_attempts (int): Number of times to retry a command before failing.

Returns:
HtdClient: The new client object.
Expand All @@ -53,6 +55,7 @@ async def async_get_client(
model_info,
network_address=network_address,
serial_address=serial_address,
retry_attempts=retry_attempts,
)

elif model_info["kind"] == HtdDeviceKind.lync:
Expand All @@ -61,10 +64,11 @@ async def async_get_client(
model_info,
network_address=network_address,
serial_address=serial_address,
retry_attempts=retry_attempts,
)

else:
raise ValueError(f"Unknown Device Kind: {model_info["kind"]}")
raise ValueError(f"Unknown Device Kind: {model_info['kind']}")

await client.async_connect()

Expand Down
15 changes: 13 additions & 2 deletions htd_client/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ async def _async_reconnect(self):


async def async_wait_until_ready(self):
pass
start_time = time.time()
while not self._ready:
if time.time() - start_time > self._socket_timeout_sec:
raise Exception("Timed out waiting for device to be ready")
await asyncio.sleep(0.1)

def has_zone_data(self, zone: int):
return zone in self._zone_data
Expand Down Expand Up @@ -284,7 +288,6 @@ def _process_next_command(self, data: bytes):

def _parse_command(self, zone, cmd, data):
if cmd == HtdCommonCommands.KEYPAD_EXISTS_RECEIVE_COMMAND:
print(f"DEBUG: inside _parse_command _zone_data id: {id(self._zone_data)}")
# if len(self._zone_data) == 0:
# this is zone 0 with all zone data
# second byte is zone 1 - 8
Expand Down Expand Up @@ -569,6 +572,10 @@ async def async_power_on(self, zone: int):
@abstractmethod
async def async_power_off(self, zone: int):
pass

@abstractmethod
async def async_set_bass(self, zone: int, bass: int):
pass

@abstractmethod
async def async_bass_up(self, zone: int):
Expand All @@ -578,6 +585,10 @@ async def async_bass_up(self, zone: int):
async def async_bass_down(self, zone: int):
pass

@abstractmethod
async def async_set_treble(self, zone: int, treble: int):
pass

@abstractmethod
async def async_treble_up(self, zone: int):
pass
Expand Down
31 changes: 23 additions & 8 deletions htd_client/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,26 @@ class HtdConstants:

VOLUME_OFFSET = MAX_RAW_VOLUME - MAX_VOLUME

MIN_BASS = -10
MAX_BASS = 10

MIN_TREBLE = -10
MAX_TREBLE = 10

MIN_BALANCE = -18
MAX_BALANCE = 18
# Lync Constants
LYNC_MIN_BASS = -10
LYNC_MAX_BASS = 10
LYNC_MIN_TREBLE = -10
LYNC_MAX_TREBLE = 10
LYNC_MIN_BALANCE = -18
LYNC_MAX_BALANCE = 18

# MCA Constants
# Raw values exposed to the user (-12 to 12)
MCA_MIN_BASS = -12
MCA_MAX_BASS = 12
MCA_MIN_TREBLE = -12
MCA_MAX_TREBLE = 12
MCA_MIN_BALANCE = -12
MCA_MAX_BALANCE = 12

# Step size exposed to user
MCA_BASS_TREBLE_STEP = 4
MCA_BALANCE_STEP = 6

# each message we get is chunked at 14 bytes
MESSAGE_CHUNK_SIZE = 14
Expand Down Expand Up @@ -214,6 +226,9 @@ class HtdLyncConstants:

BASS_COMMAND_OFFSET = 0x80
TREBLE_COMMAND_OFFSET = 0x80

STATUS_REFRESH_CODE = 0x1F



class HtdMcaCommands:
Expand Down
56 changes: 41 additions & 15 deletions htd_client/lync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ async def async_bass_up(self, zone: int):
current_zone = self.get_zone(zone)

new_bass = current_zone.bass + 1
if new_bass >= HtdConstants.MAX_BASS:
if new_bass > HtdConstants.LYNC_MAX_BASS:
return

await self.async_set_bass(zone, new_bass)
Expand All @@ -253,11 +253,12 @@ async def async_bass_down(self, zone: int):
current_zone = self.get_zone(zone)

new_bass = current_zone.bass - 1
if new_bass < HtdConstants.MIN_BASS:
if new_bass < HtdConstants.LYNC_MIN_BASS:
return

await self.async_set_bass(zone, new_bass)


async def async_set_bass(self, zone: int, bass: int):
"""
Set the bass of a zone.
Expand All @@ -266,17 +267,27 @@ async def async_set_bass(self, zone: int, bass: int):
zone (int): the zone
bass (int): the bass value to set
"""

logging.debug(f"Setting bass for zone {zone} to {bass}")

zone_info = self.get_zone(zone)

if zone_info.bass == bass:
return

return await self._async_send_and_validate(
encoded_bass = bass & 0xFF

await self._send_cmd(
zone,
HtdLyncCommands.BASS_SETTING_CONTROL_COMMAND_CODE,
encoded_bass
)

await self._async_send_and_validate(
lambda z: z.bass == bass,
zone,
HtdLyncCommands.COMMON_COMMAND_CODE,
HtdLyncCommands.BASS_SETTING_CONTROL_COMMAND_CODE,
bytearray([bass])
HtdLyncConstants.STATUS_REFRESH_CODE
)

async def async_treble_up(self, zone: int):
Expand All @@ -290,7 +301,7 @@ async def async_treble_up(self, zone: int):
current_zone = self.get_zone(zone)

new_treble = current_zone.treble + 1
if new_treble >= HtdConstants.MAX_TREBLE:
if new_treble > HtdConstants.LYNC_MAX_TREBLE:
return

await self.async_set_treble(zone, new_treble)
Expand All @@ -306,7 +317,7 @@ async def async_treble_down(self, zone: int):
current_zone = self.get_zone(zone)

new_treble = current_zone.treble - 1
if new_treble < HtdConstants.MIN_TREBLE:
if new_treble < HtdConstants.LYNC_MIN_TREBLE:
return

await self.async_set_treble(zone, new_treble)
Expand All @@ -325,12 +336,19 @@ async def async_set_treble(self, zone: int, treble: int):
if treble == zone_info.treble:
return

return await self._async_send_and_validate(
encoded_treble = treble & 0xFF

await self._send_cmd(
zone,
HtdLyncCommands.TREBLE_SETTING_CONTROL_COMMAND_CODE,
encoded_treble
)

await self._async_send_and_validate(
lambda z: z.treble == treble,
zone,
HtdLyncCommands.COMMON_COMMAND_CODE,
HtdLyncCommands.TREBLE_SETTING_CONTROL_COMMAND_CODE,
bytearray([treble])
HtdLyncConstants.STATUS_REFRESH_CODE
)

async def async_balance_left(self, zone: int):
Expand All @@ -344,7 +362,7 @@ async def async_balance_left(self, zone: int):
current_zone = self.get_zone(zone)

new_balance = current_zone.balance - 1
if new_balance < HtdConstants.MIN_BALANCE:
if new_balance < HtdConstants.LYNC_MIN_BALANCE:
return

await self.async_set_balance(zone, new_balance)
Expand All @@ -360,7 +378,7 @@ async def async_balance_right(self, zone: int):
current_zone = self.get_zone(zone)

new_balance = current_zone.balance + 1
if new_balance > HtdConstants.MAX_BALANCE:
if new_balance > HtdConstants.LYNC_MAX_BALANCE:
return

await self.async_set_balance(zone, new_balance)
Expand All @@ -379,11 +397,19 @@ async def async_set_balance(self, zone: int, balance: int):
if balance == current_zone.balance:
return

return await self._async_send_and_validate(
lambda z: z.balance == balance,
encoded_balance = balance & 0xFF

await self._send_cmd(
zone,
HtdLyncCommands.BALANCE_SETTING_CONTROL_COMMAND_CODE,
balance
encoded_balance
)

await self._async_send_and_validate(
lambda z: z.balance == balance,
zone,
HtdLyncCommands.COMMON_COMMAND_CODE,
HtdLyncConstants.STATUS_REFRESH_CODE
)

# def query_zone_name(self, zone: int) -> str:
Expand Down
Loading