Skip to content
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

(chore) Reference ConfigEntry consistently #511

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 11 additions & 11 deletions custom_components/myskoda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
]


async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up MySkoda integration from a config entry."""

trace_configs = []
if config.options.get("tracing"):
if entry.options.get("tracing"):
trace_configs.append(TRACE_CONFIG)

session = async_create_clientsession(
Expand All @@ -61,40 +61,40 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
myskoda = MySkoda(session, get_default_context(), mqtt_enabled=False)

try:
await myskoda.connect(config.data["email"], config.data["password"])
await myskoda.connect(entry.data["email"], entry.data["password"])
except AuthorizationFailedError as exc:
_LOGGER.debug("Authorization with MySkoda failed.")
raise ConfigEntryAuthFailed from exc
except (TermsAndConditionsError, MarketingConsentError) as exc:
_LOGGER.error(
"Change to terms and conditions or consents detected while logging in. Please log into the MySkoda app (may require a logout first) to access the new Terms and Conditions. This HomeAssistant integration currently can not continue."
)
async_create_tnc_issue(hass, config.entry_id)
async_create_tnc_issue(hass, entry.entry_id)
raise ConfigEntryNotReady from exc
except (CSRFError, InvalidUrlClientError) as exc:
_LOGGER.debug("An error occurred during login.")
raise ConfigEntryNotReady from exc
except ClientResponseError as err:
handle_aiohttp_error("setup", err, hass, config)
handle_aiohttp_error("setup", err, hass, entry)
except Exception:
_LOGGER.exception("Login with MySkoda failed for an unknown reason.")
return False

async_delete_tnc_issue(hass, config.entry_id)
async_delete_spin_issue(hass, config.entry_id)
async_delete_tnc_issue(hass, entry.entry_id)
async_delete_spin_issue(hass, entry.entry_id)

coordinators: dict[str, MySkodaDataUpdateCoordinator] = {}
vehicles = await myskoda.list_vehicle_vins()
for vin in vehicles:
coordinator = MySkodaDataUpdateCoordinator(hass, config, myskoda, vin)
coordinator = MySkodaDataUpdateCoordinator(hass, entry, myskoda, vin)
await coordinator.async_config_entry_first_refresh()
coordinators[vin] = coordinator

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][config.entry_id] = {COORDINATORS: coordinators}
hass.data[DOMAIN][entry.entry_id] = {COORDINATORS: coordinators}

await hass.config_entries.async_forward_entry_setups(config, PLATFORMS)
config.async_on_unload(config.add_update_listener(_async_update_listener))
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(_async_update_listener))

return True

Expand Down
2 changes: 1 addition & 1 deletion custom_components/myskoda/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def is_supported(self) -> bool:
all_capabilities_present = all(
self.vehicle.has_capability(cap) for cap in self.required_capabilities()
)
readonly = self.coordinator.config.options.get(CONF_READONLY)
readonly = self.coordinator.entry.options.get(CONF_READONLY)

return all_capabilities_present and not readonly

Expand Down
10 changes: 5 additions & 5 deletions custom_components/myskoda/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@

async def async_setup_entry(
hass: HomeAssistant,
config: ConfigEntry,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
add_supported_entities(
available_entities=[MySkodaClimate, AuxiliaryHeater],
coordinators=hass.data[DOMAIN][config.entry_id][COORDINATORS],
coordinators=hass.data[DOMAIN][entry.entry_id][COORDINATORS],
async_add_entities=async_add_entities,
)

Expand Down Expand Up @@ -190,7 +190,7 @@ def is_supported(self) -> bool:
all_capabilities_present = all(
self.vehicle.has_capability(cap) for cap in self.required_capabilities()
)
readonly = self.coordinator.config.options.get(CONF_READONLY)
readonly = self.coordinator.entry.options.get(CONF_READONLY)

return all_capabilities_present and not readonly

Expand Down Expand Up @@ -287,7 +287,7 @@ def _state(self) -> str | None:

@property
def available(self) -> bool: # noqa: D102
if not self.coordinator.config.options.get(CONF_SPIN):
if not self.coordinator.entry.options.get(CONF_SPIN):
return False
return True

Expand Down Expand Up @@ -357,7 +357,7 @@ async def handle_mode(desired_state, start_mode=None, **kwargs):
start_mode=start_mode,
**kwargs,
)
spin = self.coordinator.config.options.get(CONF_SPIN)
spin = self.coordinator.entry.options.get(CONF_SPIN)
if spin is None:
_LOGGER.error("Cannot start %s: No S-PIN set.", desired_state)
return
Expand Down
26 changes: 13 additions & 13 deletions custom_components/myskoda/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class MySkodaDataUpdateCoordinator(DataUpdateCoordinator[State]):
data: State

def __init__(
self, hass: HomeAssistant, config: ConfigEntry, myskoda: MySkoda, vin: str
self, hass: HomeAssistant, entry: ConfigEntry, myskoda: MySkoda, vin: str
) -> None:
"""Create a new coordinator."""

Expand All @@ -101,7 +101,7 @@ def __init__(
_LOGGER,
name=DOMAIN,
update_interval=timedelta(
minutes=config.options.get(
minutes=entry.options.get(
CONF_POLL_INTERVAL, DEFAULT_FETCH_INTERVAL_IN_MINUTES
)
),
Expand All @@ -111,7 +111,7 @@ def __init__(
self.vin: str = vin
self.myskoda: MySkoda = myskoda
self.operations: OrderedDict = OrderedDict()
self.config: ConfigEntry = config
self.entry: ConfigEntry = entry
self.update_driving_range = self._debounce(self._update_driving_range)
self.update_charging = self._debounce(self._update_charging)
self.update_air_conditioning = self._debounce(self._update_air_conditioning)
Expand Down Expand Up @@ -198,7 +198,7 @@ def _async_finish_startup(hass, config, vin) -> None:
try:
user = await self.myskoda.get_user()
except ClientResponseError as err:
handle_aiohttp_error("user", err, self.hass, self.config)
handle_aiohttp_error("user", err, self.hass, self.entry)
if self.data.user:
user = self.data.user
else:
Expand All @@ -208,7 +208,7 @@ def _async_finish_startup(hass, config, vin) -> None:
try:
vehicle = await self._async_get_vehicle_data()
except ClientResponseError as err:
handle_aiohttp_error("vehicle", err, self.hass, self.config)
handle_aiohttp_error("vehicle", err, self.hass, self.entry)
except ClientError as err:
raise UpdateFailed("Error getting update from MySkoda API: %s", err)

Expand Down Expand Up @@ -348,7 +348,7 @@ async def _update_driving_range(self) -> None:
try:
driving_range = await self.myskoda.get_driving_range(self.vin)
except ClientResponseError as err:
handle_aiohttp_error("driving range", err, self.hass, self.config)
handle_aiohttp_error("driving range", err, self.hass, self.entry)
except ClientError as err:
raise UpdateFailed("Error getting update from MySkoda API: %s", err)

Expand All @@ -364,7 +364,7 @@ async def _update_charging(self) -> None:
try:
charging = await self.myskoda.get_charging(self.vin)
except ClientResponseError as err:
handle_aiohttp_error("charging information", err, self.hass, self.config)
handle_aiohttp_error("charging information", err, self.hass, self.entry)
except ClientError as err:
raise UpdateFailed("Error getting update from MySkoda API: %s", err)

Expand All @@ -380,7 +380,7 @@ async def _update_air_conditioning(self) -> None:
try:
air_conditioning = await self.myskoda.get_air_conditioning(self.vin)
except ClientResponseError as err:
handle_aiohttp_error("AC update", err, self.hass, self.config)
handle_aiohttp_error("AC update", err, self.hass, self.entry)
except ClientError as err:
raise UpdateFailed("Error getting update from MySkoda API: %s", err)

Expand All @@ -401,7 +401,7 @@ async def _update_auxiliary_heating(self) -> None:
try:
auxiliary_heating = await self.myskoda.get_auxiliary_heating(self.vin)
except ClientResponseError as err:
handle_aiohttp_error("Auxiliary update", err, self.hass, self.config)
handle_aiohttp_error("Auxiliary update", err, self.hass, self.entry)
except ClientError as err:
raise UpdateFailed("Error getting update from MySkoda API: %s", err)

Expand All @@ -417,7 +417,7 @@ async def _update_status(self) -> None:
try:
status = await self.myskoda.get_status(self.vin)
except ClientResponseError as err:
handle_aiohttp_error("vehicle status", err, self.hass, self.config)
handle_aiohttp_error("vehicle status", err, self.hass, self.entry)
except ClientError as err:
raise UpdateFailed("Error getting update from MySkoda API: %s", err)

Expand All @@ -433,7 +433,7 @@ async def _update_departure_info(self) -> None:
try:
departure_info = await self.myskoda.get_departure_timers(self.vin)
except ClientResponseError as err:
handle_aiohttp_error("departure info", err, self.hass, self.config)
handle_aiohttp_error("departure info", err, self.hass, self.entry)
except ClientError as err:
raise UpdateFailed("Error getting update from MySkoda API: %s", err)

Expand All @@ -449,7 +449,7 @@ async def _update_vehicle(self) -> None:
try:
vehicle = await self.myskoda.get_vehicle(self.vin)
except ClientResponseError as err:
handle_aiohttp_error("vehicle update", err, self.hass, self.config)
handle_aiohttp_error("vehicle update", err, self.hass, self.entry)
except ClientError as err:
raise UpdateFailed("Error getting update from MySkoda API: %s", err)

Expand All @@ -468,7 +468,7 @@ async def _update_positions(self) -> None:
await asyncio.sleep(60) # GPS is not updated immediately, wait 60 seconds
positions = await self.myskoda.get_positions(self.vin)
except ClientResponseError as err:
handle_aiohttp_error("positions", err, self.hass, self.config)
handle_aiohttp_error("positions", err, self.hass, self.entry)
except ClientError as err:
raise UpdateFailed("Error getting update from MySkoda API: %s", err)

Expand Down
10 changes: 5 additions & 5 deletions custom_components/myskoda/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DoorLock(MySkodaLock):

@property
def available(self) -> bool:
if not self.coordinator.config.options.get(CONF_SPIN):
if not self.coordinator.entry.options.get(CONF_SPIN):
return False
return True

Expand All @@ -77,18 +77,18 @@ async def _async_lock_unlock(self, lock: bool, spin: str, **kwargs): # noqa: D1
_LOGGER.error("Failed to unlock vehicle: %s", exc)

async def async_lock(self, **kwargs) -> None:
if self.coordinator.config.options.get(CONF_SPIN):
if self.coordinator.entry.options.get(CONF_SPIN):
await self._async_lock_unlock(
lock=True, spin=self.coordinator.config.options.get(CONF_SPIN)
lock=True, spin=self.coordinator.entry.options.get(CONF_SPIN)
)
_LOGGER.info("Sent command to lock the vehicle.")
else:
_LOGGER.error("Cannot lock car: No S-PIN set.")

async def async_unlock(self, **kwargs) -> None:
if self.coordinator.config.options.get(CONF_SPIN):
if self.coordinator.entry.options.get(CONF_SPIN):
await self._async_lock_unlock(
lock=False, spin=self.coordinator.config.options.get(CONF_SPIN)
lock=False, spin=self.coordinator.entry.options.get(CONF_SPIN)
)
_LOGGER.info("Sent command to unlock the vehicle.")
else:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/myskoda/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def is_supported(self) -> bool:
all_capabilities_present = all(
self.vehicle.has_capability(cap) for cap in self.required_capabilities()
)
readonly = self.coordinator.config.options.get(CONF_READONLY)
readonly = self.coordinator.entry.options.get(CONF_READONLY)

return all_capabilities_present and not readonly

Expand Down
2 changes: 1 addition & 1 deletion custom_components/myskoda/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def is_supported(self) -> bool:
all_capabilities_present = all(
self.vehicle.has_capability(cap) for cap in self.required_capabilities()
)
readonly = self.coordinator.config.options.get(CONF_READONLY)
readonly = self.coordinator.entry.options.get(CONF_READONLY)

return all_capabilities_present and not readonly

Expand Down
Loading