Skip to content

Commit

Permalink
add localization of sensor value fix #28
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuen Lee committed Dec 26, 2023
1 parent aa12c0a commit c4642db
Show file tree
Hide file tree
Showing 9 changed files with 593 additions and 112 deletions.
3 changes: 3 additions & 0 deletions custom_components/polestar_api/polestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ async def init(self):
self.id = vin[:8]
self.name = "Polestar " + vin[-4:]

def get_token_expiry(self):
return self.polestarApi.auth.token_expiry

def get_latest_data(self, query: str, field_name: str):
return self.polestarApi.get_latest_data(query, field_name)

Expand Down
2 changes: 2 additions & 0 deletions custom_components/polestar_api/pypolestar/polestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ async def get_ev_data(self, vin: str):
self.updating = True

try:
if self.auth.token_expiry is None:
raise PolestarAuthException("No token expiry found")
if self.auth.token_expiry < datetime.now():
await self.auth.get_token()
except PolestarAuthException as e:
Expand Down
28 changes: 20 additions & 8 deletions custom_components/polestar_api/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class PolestarSensorDescription(
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DISTANCE,
max_value=None,
dict_data=None
dict_data=None,
),
PolestarSensorDescription(
key="estimate_distance_to_empty_km",
Expand Down Expand Up @@ -354,6 +354,17 @@ class PolestarSensorDescription(
max_value=None,
dict_data=API_STATUS_DICT
),
PolestarSensorDescription(
key="api_token_expires_at",
name="API token expired at",
icon="mdi:heart",
query=None,
field_name=None,
unit=None,
round_digits=None,
max_value=None,
dict_data=None
),

)

Expand Down Expand Up @@ -382,7 +393,7 @@ async def async_setup_entry(
PolestarSensor(device, description) for description in POLESTAR_SENSOR_TYPES
]
async_add_entities(sensors)
platform = entity_platform.current_platform.get()
entity_platform.current_platform.get()


class PolestarSensor(PolestarEntity, SensorEntity):
Expand All @@ -399,9 +410,11 @@ def __init__(self,
# get the last 4 character of the id
unique_id = device.vin[-4:]
self.entity_id = f"{POLESTAR_API_DOMAIN}.'polestar_'.{unique_id}_{description.key}"
self._attr_name = f"{description.name}"
#self._attr_name = f"{description.name}"
self._attr_unique_id = f"polestar_{unique_id}-{description.key}"
self.description = description
self.translation_key = f"polestar_{description.key}"
self.has_entity_name = True

self.entity_description = description
if description.state_class is not None:
Expand Down Expand Up @@ -430,11 +443,6 @@ def unique_id(self) -> str:
"""Return a unique ID."""
return f"{self._device.id}-{self.entity_description.key}"

@property
def name(self) -> str:
"""Return the name of the sensor."""
return self._attr_name

@property
def icon(self) -> str | None:
"""Return the icon of the sensor."""
Expand Down Expand Up @@ -462,6 +470,10 @@ def state(self) -> StateType:
return self.entity_description.dict_data.get(
self._attr_native_value, self._attr_native_value)

if self.entity_description.key == 'api_token_expires_at':
if self._device.get_token_expiry() is None:
return None
return self._device.get_token_expiry().strftime("%Y-%m-%d %H:%M:%S")
if self._attr_native_value != 0 and self._attr_native_value in (None, False):
return None

Expand Down
112 changes: 94 additions & 18 deletions custom_components/polestar_api/translations/de.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,98 @@
{
"config": {
"step": {
"user": {
"title": "Konfiguriere Deinen Polestar",
"description": "Eingabe der Zugangsdaten Deines Polestar",
"data": {
"name": "Anzeigename",
"username": "Benutzername",
"password": "Passwort",
"vin": "VIN"
}
}
},
"abort": {
"api_timeout": "Zeitüberschreitung bei der Verbindung mit der API.",
"api_failed": "Unerwarteter Fehler beim Erstellen der API.",
"already_configured": "Die Polestar API ist bereits konfiguriert",
"no_token": "Kein Token in der Antwort gefunden. Bitte überprüfe Deine Anmeldeinformationen."
"config": {
"step": {
"user": {
"title": "Konfiguriere Deinen Polestar",
"description": "Eingabe der Zugangsdaten Deines Polestar",
"data": {
"name": "Anzeigename",
"username": "Benutzername",
"password": "Passwort",
"vin": "VIN"
}
}
},
"abort": {
"api_timeout": "Zeitüberschreitung bei der Verbindung mit der API.",
"api_failed": "Unerwarteter Fehler beim Erstellen der API.",
"already_configured": "Die Polestar API ist bereits konfiguriert",
"no_token": "Kein Token in der Antwort gefunden. Bitte überprüfe Deine Anmeldeinformationen."
}
},
"entity": {
"sensor": {
"polestar_estimate_distance_to_empty_miles": {
"name": "Reichweite (Meilen)"
},
"polestar_estimate_distance_to_empty_km": {
"name": "Reichweite (km)"
},
"polestar_current_odometer_meters": {
"name": "Kilometerstand"
},
"polestar_average_speed_km_per_hour": {
"name": "Durchschnittsgeschwindigkeit (km/h)"
},
"polestar_current_trip_meter_automatic": {
"name": "Trip Meter Automatisch"
},
"polestar_current_trip_meter_manual": {
"name": "Trip Meter Manuell"
},
"polestar_battery_charge_level": {
"name": "Batterieladestand"
},
"polestar_estimated_charging_time_to_full_minutes": {
"name": "Geschätzte Ladezeit bis zur vollen Batterie"
},
"polestar_charging_status": {
"name": "Ladezustand"
},
"polestar_charging_power_watts": {
"name": "Ladeleistung"
},
"polestar_charging_current_amps": {
"name": "Ladestrom"
},
"polestar_charger_connection_status": {
"name": "Ladestatus"
},
"polestar_average_energy_consumption_kwh_per_100_km": {
"name": "Durchschnittlicher Energieverbrauch (kWh/100km)"
},
"polestar_estimated_charging_time_minutes_to_target_distance": {
"name": "Geschätzte Ladezeit bis zur Zielentfernung"
},
"polestar_vin": {
"name": "VIN"
},
"polestar_registration_number": {
"name": "Registrierungsnummer"
},
"polestar_estimated_fully_charged_time": {
"name": "Geschätzte vollständig geladene Zeit"
},
"polestar_model_name": {
"name": "Modellname"
},
"polestar_last_updated_odometer_data": {
"name": "Letzte Aktualisierung der Kilometerstanddaten"
},
"polestar_last_updated_battery_data": {
"name": "Letzte Aktualisierung der Batteriedaten"
},
"polestar_estimate_full_charge_range_miles": {
"name": "Geschätzte Reichweite bei voller Ladung (Meilen)"
},
"polestar_estimate_full_charge_range": {
"name": "Geschätzte Reichweite bei voller Ladung"
},
"polestar_api_status_code": {
"name": "API Status Code"
},
"polestar_api_token_expires_at": {
"name": "API Token läuft ab"
}
}
}
}
119 changes: 94 additions & 25 deletions custom_components/polestar_api/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,98 @@
{
"config": {
"step": {
"user": {
"title": "Configure Polestar EV",
"description": "Enter Authentication of Polestar EV",
"data": {
"name": "Friendly name",
"username": "Username",
"password": "Password",
"vin": "VIN"
}
}
},
"abort": {
"api_timeout": "Timeout connecting to the api.",
"api_failed": "Unexpected error creating api.",
"already_configured": "Polestar API is already configured",
"no_token": "No token found in response. Please check your credentials."
"config": {
"step": {
"user": {
"title": "Configure Polestar EV",
"description": "Enter Authentication of Polestar EV",
"data": {
"name": "Friendly name",
"username": "Username",
"password": "Password",
"vin": "VIN"
}
}
},
"entity": {
"sensor": {
"polestar_api_estimate_distance_to_empty_km": {
"name": "Distance km Remaining"
}
}
"abort": {
"api_timeout": "Timeout connecting to the api.",
"api_failed": "Unexpected error creating api.",
"already_configured": "Polestar API is already configured",
"no_token": "No token found in response. Please check your credentials."
}
},
"entity": {
"sensor": {
"polestar_estimate_distance_to_empty_miles": {
"name": "Distance Miles Remaining"
},
"polestar_estimate_distance_to_empty_km": {
"name": "Distance km Remaining"
},
"polestar_current_odometer_meters": {
"name": "Odometer Meters"
},
"polestar_average_speed_km_per_hour": {
"name": "Average Speed km/h"
},
"polestar_current_trip_meter_automatic": {
"name": "Trip Meter Automatic"
},
"polestar_current_trip_meter_manual": {
"name": "Trip Meter Manual"
},
"polestar_battery_charge_level": {
"name": "Battery Charge Level"
},
"polestar_estimated_charging_time_to_full_minutes": {
"name": "Estimated Charging Time to Full Minutes"
},
"polestar_charging_status": {
"name": "Charging Status"
},
"polestar_charging_power_watts": {
"name": "Charging Power Watts"
},
"polestar_charging_current_amps": {
"name": "Charging Current Amps"
},
"polestar_charger_connection_status": {
"name": "Charger Connection Status"
},
"polestar_average_energy_consumption_kwh_per_100_km": {
"name": "Average Energy Consumption kWh/100km"
},
"polestar_estimated_charging_time_minutes_to_target_distance": {
"name": "Estimated Charging Time Minutes to Target Distance"
},
"polestar_vin": {
"name": "VIN"
},
"polestar_registration_number": {
"name": "Registration Number"
},
"polestar_estimated_fully_charged_time": {
"name": "Estimated Fully Charged Time"
},
"polestar_model_name": {
"name": "Model Name"
},
"polestar_last_updated_odometer_data": {
"name": "Last Updated Odometer Data"
},
"polestar_last_updated_battery_data": {
"name": "Last Updated Battery Data"
},
"polestar_estimate_full_charge_range_miles": {
"name": "Estimate Full Charge Range Miles"
},
"polestar_estimate_full_charge_range": {
"name": "Estimate Full Charge Range"
},
"polestar_api_status_code": {
"name": "API Status Code"
},
"polestar_api_token_expires_at": {
"name": "API Token Expires At"
}
}
}
}
}
Loading

0 comments on commit c4642db

Please sign in to comment.