Skip to content

Commit

Permalink
Fix for duplicate calendar events
Browse files Browse the repository at this point in the history
Removed `async_setup_platform` from sensors as it is redundant.

Simplified event descriptions to include just source and reference so it can be used to fetch data from other entities.
  • Loading branch information
jampez77 committed Sep 12, 2024
1 parent 3a9305f commit 5b83908
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 136 deletions.
7 changes: 3 additions & 4 deletions custom_components/jet2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@

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

hass.data.setdefault(DOMAIN, {})
hass_data = dict(entry.data)

# Register services when the first config entry is added
if not hass.data[DOMAIN]:
async_setup_services(hass)

# Registers update listener to update config entry when options are updated.
unsub_options_update_listener = entry.add_update_listener(options_update_listener)
# Store a reference to the unsubscribe function to cleanup if an entry is unloaded.
hass_data["unsub_options_update_listener"] = unsub_options_update_listener
hass.data[DOMAIN][entry.entry_id] = hass_data
# Forward the setup to each platform.

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True


Expand Down
20 changes: 0 additions & 20 deletions custom_components/jet2/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from homeassistant.core import HomeAssistant
from typing import Any
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import DOMAIN, CONF_BOOKING_REFERENCE
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity import DeviceInfo
Expand Down Expand Up @@ -64,25 +63,6 @@ async def async_setup_entry(
async_add_entities(sensors, update_before_add=True)


async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
_: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
session = async_get_clientsession(hass)

coordinator = Jet2Coordinator(hass, session, config)

name = config[CONF_BOOKING_REFERENCE]

sensors = [
Jet2BinarySensor(coordinator, name, description) for description in SENSOR_TYPES
]
async_add_entities(sensors, update_before_add=True)


class Jet2BinarySensor(CoordinatorEntity[Jet2Coordinator], BinarySensorEntity):
"""Define an Jet2 sensor."""

Expand Down
73 changes: 4 additions & 69 deletions custom_components/jet2/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

from datetime import datetime
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import DOMAIN, CONF_BOOKING_REFERENCE, CONF_CALENDARS
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import ConfigEntry, ConfigType
from homeassistant.components.calendar import CalendarEntity, CalendarEvent
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
Expand Down Expand Up @@ -57,6 +56,7 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up sensors from a config entry created in the integrations UI."""

config = hass.data[DOMAIN][entry.entry_id]
# Update our config to include new repos and remove those that have been removed.
if entry.options:
Expand Down Expand Up @@ -84,33 +84,6 @@ async def async_setup_entry(
async_add_entities(sensors, update_before_add=True)


async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
_: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
session = async_get_clientsession(hass)
coordinator = Jet2Coordinator(hass, session, config)

name = config[CONF_BOOKING_REFERENCE]

calendars = config[CONF_CALENDARS]

sensors = [Jet2CalendarSensor(coordinator, name)]

for calendar in calendars:
if calendar != "None":
for sensor in sensors:
events = sensor.get_events(datetime.today(), hass)
for event in events:
await add_to_calendar(hass, calendar, event, config)

if "None" in calendars:
async_add_entities(sensors, update_before_add=True)


async def create_event(hass: HomeAssistant, service_data):
"""Create calendar event."""
try:
Expand Down Expand Up @@ -209,6 +182,7 @@ async def add_to_calendar(
if created_event_uid is not None and created_event_uid not in uids:
uids.append(created_event_uid)

if uids != entry.data.get("uids", []):
updated_data = entry.data.copy()
updated_data["uids"] = uids
hass.config_entries.async_update_entry(entry, data=updated_data)
Expand Down Expand Up @@ -256,10 +230,7 @@ def get_events(
event_end_raw = None
event_name = date_sensor_type.name
event_location = event_name
event_description = {
"source": "Jet2",
"reference": self.data["bookingReference"],
}
event_description = f"Jet2|{self.data["bookingReference"]}"

if (
date_sensor_type.key == "priceBreakdown"
Expand All @@ -280,48 +251,12 @@ def get_events(
if "outbound" in flightSummary:
outbound = flightSummary["outbound"]

event_description["outbound"] = {
"flightNumber": outbound["number"],
"duration": outbound["duration"],
"departure": {
"airport": outbound["departureAirport"],
"localDepartureDateTime": outbound[
"localDepartureDateTime"
],
"terminal": outbound["departureTerminal"],
},
"arrival": {
"airport": outbound["arrivalAirport"],
"localArrivalDateTime": outbound[
"localArrivalDateTime"
],
"terminal": outbound["arrivalTerminal"],
},
}

if "localDepartureDateTime" in outbound:
event_start_raw = outbound["localDepartureDateTime"]

if "inbound" in flightSummary:
inbound = flightSummary["inbound"]

event_description["inbound"] = {
"flightNumber": inbound["number"],
"duration": inbound["duration"],
"departure": {
"airport": inbound["departureAirport"],
"localDepartureDateTime": inbound[
"localDepartureDateTime"
],
"terminal": inbound["departureTerminal"],
},
"arrival": {
"airport": inbound["arrivalAirport"],
"localArrivalDateTime": inbound["localArrivalDateTime"],
"terminal": inbound["arrivalTerminal"],
},
}

if "localArrivalDateTime" in inbound:
event_end_raw = inbound["localArrivalDateTime"]

Expand Down
20 changes: 0 additions & 20 deletions custom_components/jet2/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,6 @@ async def async_setup_entry(
async_add_entities(sensors, update_before_add=True)


async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
_: DiscoveryInfoType | None = None,
) -> None:
"""Set up the custom camera platform."""

session = async_get_clientsession(hass)

coordinator = Jet2Coordinator(hass, session, config)

await coordinator.async_refresh()

name = config[CONF_BOOKING_REFERENCE]

sensors = [Jet2CameraSensor(coordinator, name, SENSOR_DESCRIPTION)]
async_add_entities(sensors, update_before_add=True)


class Jet2CameraSensor(CoordinatorEntity[Jet2Coordinator], Camera):
"""Representation of a Camera entity."""

Expand Down
2 changes: 1 addition & 1 deletion custom_components/jet2/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"issue_tracker": "https://github.com/jampez77/Jet2/issues",
"requirements": [],
"ssdp": [],
"version": "2024.9.5",
"version": "2024.9.6",
"zeroconf": []
}
21 changes: 0 additions & 21 deletions custom_components/jet2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,6 @@ async def async_setup_entry(
async_add_entities(sensors, update_before_add=True)


async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
_: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
session = async_get_clientsession(hass)
coordinator = Jet2Coordinator(hass, session, config)

name = config[CONF_BOOKING_REFERENCE]

if hasBookingExpired(hass, coordinator.data.get("data")["expiryDate"]):
await removeBooking(hass, name)
else:
sensors = [
Jet2Sensor(coordinator, name, description) for description in SENSOR_TYPES
]
async_add_entities(sensors, update_before_add=True)


class Jet2Sensor(CoordinatorEntity[Jet2Coordinator], SensorEntity):
"""Define an Jet2 sensor."""

Expand Down
1 change: 0 additions & 1 deletion custom_components/jet2/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Setup the service (if it hasn't already been set up globally)
async_setup_services(hass)

# You may also register entities, update the coordinator, etc.
await coordinator.async_refresh()

if coordinator.last_exception is not None:
Expand Down

0 comments on commit 5b83908

Please sign in to comment.