Skip to content

Commit

Permalink
HACS update to Next Rocket Launch #16
Browse files Browse the repository at this point in the history
  • Loading branch information
BeardedTinker committed Dec 2, 2023
1 parent a40305a commit 59ff0f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
10 changes: 7 additions & 3 deletions custom_components/next_rocket_launch/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
"issue_tracker": "https://github.com/Verbalinsurection/next_rocket_launch/issues",
"version": "v1.0.5",
"config_flow": false,
"requirements": ["ics==0.7"],
"requirements": [
"ics==0.7.2"
],
"ssdp": [],
"zeroconf": [],
"homekit": {},
"dependencies": [],
"codeowners": ["@Verbalinsurection"]
}
"codeowners": [
"@Verbalinsurection"
]
}
30 changes: 12 additions & 18 deletions custom_components/next_rocket_launch/sensor.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
"""The Next Rocket Launch integration."""

from datetime import datetime, timedelta, timezone
import asyncio
from datetime import UTC, datetime, timedelta
import logging

from ics import Calendar
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.helpers.aiohttp_client import async_create_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.aiohttp_client import async_create_clientsession
import async_timeout
from homeassistant.util import Throttle

_LOGGER = logging.getLogger(__name__)

Expand All @@ -32,11 +32,12 @@

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Create the launch sensor."""

session = async_create_clientsession(hass)
ics_data_provider = GetICSData(ICS_URL, session, hass)

async def async_update_data():
async with async_timeout.timeout(10):
async with asyncio.timeout(10):
return await ics_data_provider.ics_update()

coordinator = DataUpdateCoordinator(
Expand All @@ -57,7 +58,6 @@ async def async_update_data():


class GetICSData:

"""The class for handling the data retrieval."""

def __init__(self, url, session, hass):
Expand All @@ -73,7 +73,7 @@ async def ics_update(self):
"""Get the latest data from ics."""
_LOGGER.debug("Get the latest data from ics")

async with async_timeout.timeout(10):
async with asyncio.timeout(10):
resp = await self.session.get(self.url)

if resp.status != 200:
Expand Down Expand Up @@ -106,7 +106,6 @@ async def ics_update(self):


class GetNextLaunch(Entity):

"""The class for handling the data."""

def __init__(self, coordinator, rocket_name, ics_data_provider):
Expand All @@ -122,6 +121,7 @@ def __init__(self, coordinator, rocket_name, ics_data_provider):

async def async_update(self):
"""Process data."""

_LOGGER.debug("Start async update for %s", self.name)

self.have_futur = False
Expand All @@ -145,12 +145,11 @@ async def async_update(self):
]

for event in selected_events:
if event.begin < datetime.now(timezone.utc):
if event.begin < datetime.now(UTC):
last_passed = event
else:
if not self.have_futur:
last_futur = event
self.have_futur = True
elif not self.have_futur:
last_futur = event
self.have_futur = True

if last_futur is not None:
self._state = last_futur.begin.isoformat()
Expand Down Expand Up @@ -207,8 +206,3 @@ async def async_added_to_hass(self):
self.async_on_remove(
self.coordinator.async_add_listener(self.async_write_ha_state)
)

# @property
# def should_poll(self):
# """No need to poll. Coordinator notifies entity of updates."""
# return False

0 comments on commit 59ff0f3

Please sign in to comment.