Skip to content

Commit

Permalink
improved spin handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tillsteinbach committed Jan 23, 2025
1 parent fe751ae commit dac6f62
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/carconnectivity_connectors/skoda/command_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ def value(self, new_value: Optional[Union[str, Dict]]) -> None:
parser = ThrowingArgumentParser(prog='', add_help=False, exit_on_error=False)
parser.add_argument('command', help='Command to execute', type=SpinCommand.Command,
choices=list(SpinCommand.Command))
parser.add_argument('--spin', dest='spin', help='Spin to be used instead of spin from config or .netrc', type=str, required=False,
default=None)
try:
args = parser.parse_args(new_value.split(sep=' '))
except argparse.ArgumentError as e:
raise SetterError(f'Invalid format for SpinCommand: {e.message} {parser.format_usage()}') from e

newvalue_dict = {}
newvalue_dict['command'] = args.command
if args.spin is not None:
newvalue_dict['spin'] = args.spin
new_value = newvalue_dict
elif isinstance(new_value, dict):
if 'command' in new_value and isinstance(new_value['command'], str):
Expand Down
26 changes: 18 additions & 8 deletions src/carconnectivity_connectors/skoda/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from carconnectivity.position import Position
from carconnectivity.climatization import Climatization
from carconnectivity.charging_connector import ChargingConnector
from carconnectivity.commands import Commands
from carconnectivity.command_impl import ClimatizationStartStopCommand, ChargingStartStopCommand, HonkAndFlashCommand, LockUnlockCommand

from carconnectivity_connectors.base.connector import BaseConnector
Expand Down Expand Up @@ -70,6 +71,7 @@ def __init__(self, connector_id: str, car_connectivity: CarConnectivity, config:

self.connected: BooleanAttribute = BooleanAttribute(name="connected", parent=self)
self.interval: DurationAttribute = DurationAttribute(name="interval", parent=self)
self.commands: Commands = Commands(parent=self)

self.user_id: Optional[str] = None

Expand Down Expand Up @@ -906,7 +908,7 @@ def fetch_vehicle_details(self, vehicle: SkodaVehicle, no_cache: bool = False) -
# Add spin command
if vehicle.commands is not None and vehicle.commands.commands is not None \
and not vehicle.commands.contains_command('spin'):
spin_command = SpinCommand(parent=vehicle.commands)
spin_command = SpinCommand(parent=self.commands)
spin_command._add_on_set_hook(self.__on_spin) # pylint: disable=protected-access
spin_command.enabled = True
vehicle.commands.add_command(spin_command)
Expand Down Expand Up @@ -1553,7 +1555,7 @@ def __on_honk_flash(self, honk_flash_command: HonkAndFlashCommand, command_argum
command_dict['vehiclePosition']['latitude'] = vehicle.position.latitude.value
command_dict['vehiclePosition']['longitude'] = vehicle.position.longitude.value

url = f'https://mysmob.api.connect.skoda-auto.cz/v1/vehicle-access/{vin}/honk-and-flash'
url = f'https://mysmob.api.connect.skoda-auto.cz/api/v1/vehicle-access/{vin}/honk-and-flash'
command_response: requests.Response = self.session.post(url, data=json.dumps(command_dict), allow_redirects=True)
if command_response.status_code != requests.codes['accepted']:
LOG.error('Could not execute honk or flash command (%s: %s)', command_response.status_code, command_response.text)
Expand All @@ -1576,13 +1578,16 @@ def __on_lock_unlock(self, lock_unlock_command: LockUnlockCommand, command_argum
if 'command' not in command_arguments:
raise SetterError('Command argument missing')
command_dict = {}
if self._spin is None:
raise SetterError('S-PIN is missing, please add S-PIN to your configuration or .netrc file')
command_dict['currentSpin'] = self._spin
if 'spin' in command_arguments:
command_dict['currentSpin'] = command_arguments['spin']
else:
if self._spin is None:
raise SetterError('S-PIN is missing, please add S-PIN to your configuration or .netrc file')
command_dict['currentSpin'] = self._spin
if command_arguments['command'] == LockUnlockCommand.Command.LOCK:
url = f'https://mysmob.api.connect.skoda-auto.cz/v1/vehicle-access/{vin}/lock'
url = f'https://mysmob.api.connect.skoda-auto.cz/api/v1/vehicle-access/{vin}/lock'
elif command_arguments['command'] == LockUnlockCommand.Command.UNLOCK:
url = f'https://mysmob.api.connect.skoda-auto.cz/v1/vehicle-access/{vin}/unlock'
url = f'https://mysmob.api.connect.skoda-auto.cz/api/v1/vehicle-access/{vin}/unlock'
else:
raise SetterError(f'Unknown command {command_arguments["command"]}')
command_response: requests.Response = self.session.post(url, data=json.dumps(command_dict), allow_redirects=True)
Expand All @@ -1606,7 +1611,12 @@ def __on_spin(self, spin_command: SpinCommand, command_arguments: Union[str, Dic
command_dict = {}
if self._spin is None:
raise SetterError('S-PIN is missing, please add S-PIN to your configuration or .netrc file')
command_dict['currentSpin'] = self._spin
if 'spin' in command_arguments:
command_dict['currentSpin'] = command_arguments['spin']
else:
if self._spin is None:
raise SetterError('S-PIN is missing, please add S-PIN to your configuration or .netrc file')
command_dict['currentSpin'] = self._spin
if command_arguments['command'] == SpinCommand.Command.VERIFY:
url = 'https://mysmob.api.connect.skoda-auto.cz/v1/spin/verify'
else:
Expand Down

0 comments on commit dac6f62

Please sign in to comment.