-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added support for multiple accounts #206
Changes from all commits
40f2f58
a7d1980
43fcda2
11270d8
19ea8fc
9d1ec2d
4b7cf92
5d85d93
6048796
3b9fa0f
906696e
eb06925
9ab6144
d093241
4771739
5d48770
32d7d52
3c7ab5e
8ac5c49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
CONF_USERNAME, | ||
CONF_UNIT_OF_MEASUREMENT, | ||
CONF_REGION, | ||
ATTR_DEVICE_ID, | ||
) | ||
from homeassistant.helpers.dispatcher import ( | ||
async_dispatcher_connect, | ||
|
@@ -41,6 +42,7 @@ | |
CONF_NO_FORCE_SCAN_HOUR_START, | ||
CONF_SCAN_INTERVAL, | ||
CONF_STORED_CREDENTIALS, | ||
CONF_VEHICLE_IDENTIFIER, | ||
DISTANCE_UNITS, | ||
DEFAULT_NO_FORCE_SCAN_HOUR_FINISH, | ||
DEFAULT_NO_FORCE_SCAN_HOUR_START, | ||
|
@@ -71,15 +73,30 @@ | |
|
||
|
||
async def async_setup(hass: HomeAssistant, config_entry: ConfigEntry): | ||
if DOMAIN not in hass.data: | ||
hass.data[DOMAIN] = {} | ||
hass.data.setdefault(DOMAIN, {}) | ||
|
||
def get_vehicle_identifier_from_vehicle_id(call) -> Vehicle: | ||
vehicle_identifiers = list(hass.data[DOMAIN].keys()) | ||
if len(vehicle_identifiers) == 1: | ||
vehicle_identifier = vehicle_identifiers[0] | ||
else: | ||
vehicle_identifier = get_vehicle_identifier_from_device_id( | ||
call.data[ATTR_DEVICE_ID] | ||
) | ||
|
||
return hass.data[DOMAIN][vehicle_identifier][DATA_VEHICLE_INSTANCE] | ||
|
||
def get_vehicle_identifier_from_device_id(device_id: str) -> str: | ||
device_registry = dr.async_get(hass) | ||
device_entry: dr.DeviceEntry = device_registry.async_get(device_id) | ||
return list(device_entry.identifiers.copy().pop())[1] | ||
|
||
async def async_handle_force_update(call): | ||
vehicle: Vehicle = hass.data[DOMAIN][DATA_VEHICLE_INSTANCE] | ||
vehicle: Vehicle = get_vehicle_identifier_from_vehicle_id(call) | ||
await vehicle.force_update() | ||
|
||
async def async_handle_update(call): | ||
vehicle: Vehicle = hass.data[DOMAIN][DATA_VEHICLE_INSTANCE] | ||
vehicle: Vehicle = get_vehicle_identifier_from_vehicle_id(call) | ||
await vehicle.update() | ||
|
||
async def async_handle_start_climate(call): | ||
|
@@ -88,25 +105,25 @@ async def async_handle_start_climate(call): | |
defrost = call.data.get("Defrost") | ||
climate = call.data.get("Climate") | ||
heating = call.data.get("Heating") | ||
vehicle: Vehicle = hass.data[DOMAIN][DATA_VEHICLE_INSTANCE] | ||
vehicle: Vehicle = get_vehicle_identifier_from_vehicle_id(call) | ||
await vehicle.start_climate(set_temp, duration, defrost, climate, heating) | ||
|
||
async def async_handle_stop_climate(call): | ||
vehicle: Vehicle = hass.data[DOMAIN][DATA_VEHICLE_INSTANCE] | ||
vehicle: Vehicle = get_vehicle_identifier_from_vehicle_id(call) | ||
await vehicle.stop_climate() | ||
|
||
async def async_handle_start_charge(call): | ||
vehicle: Vehicle = hass.data[DOMAIN][DATA_VEHICLE_INSTANCE] | ||
vehicle: Vehicle = get_vehicle_identifier_from_vehicle_id(call) | ||
await vehicle.start_charge() | ||
|
||
async def async_handle_stop_charge(call): | ||
vehicle: Vehicle = hass.data[DOMAIN][DATA_VEHICLE_INSTANCE] | ||
vehicle: Vehicle = get_vehicle_identifier_from_vehicle_id(call) | ||
await vehicle.stop_charge() | ||
|
||
async def async_handle_set_charge_limits(call): | ||
ac_limit = call.data.get("ac_limit") | ||
dc_limit = call.data.get("dc_limit") | ||
vehicle: Vehicle = hass.data[DOMAIN][DATA_VEHICLE_INSTANCE] | ||
vehicle: Vehicle = get_vehicle_identifier_from_vehicle_id(call) | ||
await vehicle.set_charge_limits(ac_limit, dc_limit) | ||
|
||
hass.services.async_register(DOMAIN, "force_update", async_handle_force_update) | ||
|
@@ -124,6 +141,7 @@ async def async_handle_set_charge_limits(call): | |
|
||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): | ||
_LOGGER.debug(f"{DOMAIN} - async_setup_entry started - {config_entry}") | ||
vehicle_identifier = config_entry.data[CONF_VEHICLE_IDENTIFIER] | ||
username = config_entry.data.get(CONF_USERNAME) | ||
password = config_entry.data.get(CONF_PASSWORD) | ||
pin = config_entry.data.get(CONF_PIN, DEFAULT_PIN) | ||
|
@@ -193,8 +211,10 @@ async def refresh_config_entry(): | |
|
||
async def update(event_time_utc: datetime): | ||
await refresh_config_entry() | ||
# await vehicle.refresh_token() | ||
local_timezone = vehicle.kia_uvo_api.get_timezone_by_region() | ||
event_time_local = dt_util.as_local(event_time_utc) | ||
|
||
await vehicle.update() | ||
call_force_update = False | ||
|
||
|
@@ -225,7 +245,7 @@ async def update(event_time_utc: datetime): | |
data[DATA_CONFIG_UPDATE_LISTENER] = config_entry.add_update_listener( | ||
async_update_options | ||
) | ||
hass.data[DOMAIN] = data | ||
hass.data[DOMAIN][vehicle_identifier] = data | ||
|
||
return True | ||
|
||
|
@@ -244,12 +264,30 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry): | |
) | ||
) | ||
if unload_ok: | ||
vehicle_topic_listener = hass.data[DOMAIN][DATA_VEHICLE_LISTENER] | ||
vehicle_topic_listener() | ||
vehicle_identifier = config_entry.data[CONF_VEHICLE_IDENTIFIER] | ||
vehicle_listener = hass.data[DOMAIN][vehicle_identifier][DATA_VEHICLE_LISTENER] | ||
vehicle_listener() | ||
|
||
config_update_listener = hass.data[DOMAIN][DATA_CONFIG_UPDATE_LISTENER] | ||
config_update_listener = hass.data[DOMAIN][vehicle_identifier][ | ||
DATA_CONFIG_UPDATE_LISTENER | ||
] | ||
config_update_listener() | ||
|
||
hass.data[DOMAIN] = None | ||
hass.data[DOMAIN][vehicle_identifier] = None | ||
|
||
return unload_ok | ||
|
||
|
||
async def async_migrate_entry(hass, config_entry: ConfigEntry): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very nice approach, thanks for saving existing users |
||
|
||
if config_entry.version == 1: | ||
|
||
vehicle_id = config_entry.data["stored_credentials"]["vehicle_id"] | ||
new_data = config_entry.data.copy() | ||
new_data[CONF_VEHICLE_IDENTIFIER] = vehicle_id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looking here, vehicle_id should be equal to device_id. So, are these equal? |
||
|
||
hass.config_entries.async_update_entry(config_entry, data=new_data) | ||
|
||
config_entry.version = 2 | ||
_LOGGER.info("Migration to version %s successful", config_entry.version) | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would get rid of
if
condition here,else
should work for all conditions.