This reverts commit 16a5275461
.
This commit is contained in:
parent
f237bb14ca
commit
6bb7a7225d
10 changed files with 25 additions and 131 deletions
|
@ -977,7 +977,6 @@ omit =
|
||||||
homeassistant/components/rest/notify.py
|
homeassistant/components/rest/notify.py
|
||||||
homeassistant/components/rest/switch.py
|
homeassistant/components/rest/switch.py
|
||||||
homeassistant/components/ridwell/__init__.py
|
homeassistant/components/ridwell/__init__.py
|
||||||
homeassistant/components/ridwell/calendar.py
|
|
||||||
homeassistant/components/ridwell/coordinator.py
|
homeassistant/components/ridwell/coordinator.py
|
||||||
homeassistant/components/ridwell/switch.py
|
homeassistant/components/ridwell/switch.py
|
||||||
homeassistant/components/ring/camera.py
|
homeassistant/components/ring/camera.py
|
||||||
|
|
|
@ -11,7 +11,7 @@ from homeassistant.helpers import entity_registry as er
|
||||||
from .const import DOMAIN, LOGGER, SENSOR_TYPE_NEXT_PICKUP
|
from .const import DOMAIN, LOGGER, SENSOR_TYPE_NEXT_PICKUP
|
||||||
from .coordinator import RidwellDataUpdateCoordinator
|
from .coordinator import RidwellDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.CALENDAR, Platform.SENSOR, Platform.SWITCH]
|
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.SWITCH]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
|
|
@ -1,78 +0,0 @@
|
||||||
"""Support for Ridwell calendars."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
from aioridwell.model import RidwellAccount, RidwellPickupEvent
|
|
||||||
|
|
||||||
from homeassistant.components.calendar import CalendarEntity, CalendarEvent
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
|
||||||
from .coordinator import RidwellDataUpdateCoordinator
|
|
||||||
from .entity import RidwellEntity
|
|
||||||
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def async_get_calendar_event_from_pickup_event(
|
|
||||||
pickup_event: RidwellPickupEvent,
|
|
||||||
) -> CalendarEvent:
|
|
||||||
"""Get a HASS CalendarEvent from an aiorecollect PickupEvent."""
|
|
||||||
pickup_type_string = ", ".join(
|
|
||||||
[
|
|
||||||
f"{pickup.name} (quantity: {pickup.quantity})"
|
|
||||||
for pickup in pickup_event.pickups
|
|
||||||
]
|
|
||||||
)
|
|
||||||
return CalendarEvent(
|
|
||||||
summary=f"Ridwell Pickup ({pickup_event.state.value})",
|
|
||||||
description=f"Pickup types: {pickup_type_string}",
|
|
||||||
start=pickup_event.pickup_date,
|
|
||||||
end=pickup_event.pickup_date + datetime.timedelta(days=1),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
||||||
) -> None:
|
|
||||||
"""Set up Ridwell calendars based on a config entry."""
|
|
||||||
coordinator: RidwellDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
async_add_entities(
|
|
||||||
RidwellCalendar(coordinator, account)
|
|
||||||
for account in coordinator.accounts.values()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class RidwellCalendar(RidwellEntity, CalendarEntity):
|
|
||||||
"""Define a Ridwell calendar."""
|
|
||||||
|
|
||||||
_attr_icon = "mdi:delete-empty"
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, coordinator: RidwellDataUpdateCoordinator, account: RidwellAccount
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the Ridwell entity."""
|
|
||||||
super().__init__(coordinator, account)
|
|
||||||
|
|
||||||
self._attr_unique_id = self._account.account_id
|
|
||||||
self._event: CalendarEvent | None = None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def event(self) -> CalendarEvent | None:
|
|
||||||
"""Return the next upcoming event."""
|
|
||||||
return async_get_calendar_event_from_pickup_event(self.next_pickup_event)
|
|
||||||
|
|
||||||
async def async_get_events(
|
|
||||||
self,
|
|
||||||
hass: HomeAssistant,
|
|
||||||
start_date: datetime.datetime,
|
|
||||||
end_date: datetime.datetime,
|
|
||||||
) -> list[CalendarEvent]:
|
|
||||||
"""Return calendar events within a datetime range."""
|
|
||||||
return [
|
|
||||||
async_get_calendar_event_from_pickup_event(event)
|
|
||||||
for event in self.coordinator.data[self._account.account_id]
|
|
||||||
]
|
|
|
@ -22,14 +22,14 @@ UPDATE_INTERVAL = timedelta(hours=1)
|
||||||
|
|
||||||
|
|
||||||
class RidwellDataUpdateCoordinator(
|
class RidwellDataUpdateCoordinator(
|
||||||
DataUpdateCoordinator[dict[str, list[RidwellPickupEvent]]]
|
DataUpdateCoordinator[dict[str, RidwellPickupEvent]]
|
||||||
):
|
):
|
||||||
"""Class to manage fetching data from single endpoint."""
|
"""Class to manage fetching data from single endpoint."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: ConfigEntry
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, *, name: str) -> None:
|
def __init__(self, hass: HomeAssistant, *, name: str) -> None:
|
||||||
"""Initialize."""
|
"""Initialize global data updater."""
|
||||||
# These will be filled in by async_initialize; we give them these defaults to
|
# These will be filled in by async_initialize; we give them these defaults to
|
||||||
# avoid arduous typing checks down the line:
|
# avoid arduous typing checks down the line:
|
||||||
self.accounts: dict[str, RidwellAccount] = {}
|
self.accounts: dict[str, RidwellAccount] = {}
|
||||||
|
@ -38,13 +38,13 @@ class RidwellDataUpdateCoordinator(
|
||||||
|
|
||||||
super().__init__(hass, LOGGER, name=name, update_interval=UPDATE_INTERVAL)
|
super().__init__(hass, LOGGER, name=name, update_interval=UPDATE_INTERVAL)
|
||||||
|
|
||||||
async def _async_update_data(self) -> dict[str, list[RidwellPickupEvent]]:
|
async def _async_update_data(self) -> dict[str, RidwellPickupEvent]:
|
||||||
"""Fetch the latest data from the source."""
|
"""Fetch the latest data from the source."""
|
||||||
data = {}
|
data = {}
|
||||||
|
|
||||||
async def async_get_pickups(account: RidwellAccount) -> None:
|
async def async_get_pickups(account: RidwellAccount) -> None:
|
||||||
"""Get the latest pickups for an account."""
|
"""Get the latest pickups for an account."""
|
||||||
data[account.account_id] = await account.async_get_pickup_events()
|
data[account.account_id] = await account.async_get_next_pickup_event()
|
||||||
|
|
||||||
tasks = [async_get_pickups(account) for account in self.accounts.values()]
|
tasks = [async_get_pickups(account) for account in self.accounts.values()]
|
||||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
|
|
@ -32,11 +32,7 @@ async def async_get_config_entry_diagnostics(
|
||||||
return async_redact_data(
|
return async_redact_data(
|
||||||
{
|
{
|
||||||
"entry": entry.as_dict(),
|
"entry": entry.as_dict(),
|
||||||
"data": [
|
"data": [dataclasses.asdict(event) for event in coordinator.data.values()],
|
||||||
dataclasses.asdict(event)
|
|
||||||
for events in coordinator.data.values()
|
|
||||||
for event in events
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
TO_REDACT,
|
TO_REDACT,
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
"""Define a base Ridwell entity."""
|
"""Define a base Ridwell entity."""
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from datetime import date
|
|
||||||
|
|
||||||
from aioridwell.model import RidwellAccount, RidwellPickupEvent
|
from aioridwell.model import RidwellAccount, RidwellPickupEvent
|
||||||
|
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
@ -22,6 +18,7 @@ class RidwellEntity(CoordinatorEntity[RidwellDataUpdateCoordinator]):
|
||||||
self,
|
self,
|
||||||
coordinator: RidwellDataUpdateCoordinator,
|
coordinator: RidwellDataUpdateCoordinator,
|
||||||
account: RidwellAccount,
|
account: RidwellAccount,
|
||||||
|
description: EntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
@ -34,12 +31,10 @@ class RidwellEntity(CoordinatorEntity[RidwellDataUpdateCoordinator]):
|
||||||
manufacturer="Ridwell",
|
manufacturer="Ridwell",
|
||||||
name="Ridwell",
|
name="Ridwell",
|
||||||
)
|
)
|
||||||
|
self._attr_unique_id = f"{account.account_id}_{description.key}"
|
||||||
|
self.entity_description = description
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def next_pickup_event(self) -> RidwellPickupEvent:
|
def next_pickup_event(self) -> RidwellPickupEvent:
|
||||||
"""Get the next pickup event."""
|
"""Get the next pickup event."""
|
||||||
return next(
|
return self.coordinator.data[self._account.account_id]
|
||||||
event
|
|
||||||
for event in self.coordinator.data[self._account.account_id]
|
|
||||||
if event.pickup_date >= date.today()
|
|
||||||
)
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ ATTR_QUANTITY = "quantity"
|
||||||
|
|
||||||
SENSOR_DESCRIPTION = SensorEntityDescription(
|
SENSOR_DESCRIPTION = SensorEntityDescription(
|
||||||
key=SENSOR_TYPE_NEXT_PICKUP,
|
key=SENSOR_TYPE_NEXT_PICKUP,
|
||||||
name="Next Ridwell pickup",
|
name="Ridwell pickup",
|
||||||
device_class=SensorDeviceClass.DATE,
|
device_class=SensorDeviceClass.DATE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -54,10 +54,9 @@ class RidwellSensor(RidwellEntity, SensorEntity):
|
||||||
description: SensorEntityDescription,
|
description: SensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
super().__init__(coordinator, account)
|
super().__init__(coordinator, account, description)
|
||||||
|
|
||||||
self._attr_unique_id = f"{account.account_id}_{description.key}"
|
self._attr_name = f"{description.name} ({account.address['street1']})"
|
||||||
self.entity_description = description
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> Mapping[str, Any]:
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
||||||
|
|
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aioridwell.errors import RidwellError
|
from aioridwell.errors import RidwellError
|
||||||
from aioridwell.model import EventState, RidwellAccount
|
from aioridwell.model import EventState
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
@ -38,19 +38,7 @@ async def async_setup_entry(
|
||||||
|
|
||||||
|
|
||||||
class RidwellSwitch(RidwellEntity, SwitchEntity):
|
class RidwellSwitch(RidwellEntity, SwitchEntity):
|
||||||
"""Define a Ridwell switch."""
|
"""Define a Ridwell button."""
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
coordinator: RidwellDataUpdateCoordinator,
|
|
||||||
account: RidwellAccount,
|
|
||||||
description: SwitchEntityDescription,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize."""
|
|
||||||
super().__init__(coordinator, account)
|
|
||||||
|
|
||||||
self._attr_unique_id = f"{account.account_id}_{description.key}"
|
|
||||||
self.entity_description = description
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
|
|
|
@ -3,7 +3,6 @@ from datetime import date
|
||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
|
||||||
from aioridwell.model import EventState, RidwellPickup, RidwellPickupEvent
|
from aioridwell.model import EventState, RidwellPickup, RidwellPickupEvent
|
||||||
from freezegun import freeze_time
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.ridwell.const import DOMAIN
|
from homeassistant.components.ridwell.const import DOMAIN
|
||||||
|
@ -29,16 +28,14 @@ def account_fixture():
|
||||||
"state": "New York",
|
"state": "New York",
|
||||||
"postal_code": "10001",
|
"postal_code": "10001",
|
||||||
},
|
},
|
||||||
async_get_pickup_events=AsyncMock(
|
async_get_next_pickup_event=AsyncMock(
|
||||||
return_value=[
|
return_value=RidwellPickupEvent(
|
||||||
RidwellPickupEvent(
|
None,
|
||||||
None,
|
"event_123",
|
||||||
"event_123",
|
date(2022, 1, 24),
|
||||||
date(2022, 1, 24),
|
[RidwellPickup("Plastic Film", "offer_123", 1, "product_123", 1)],
|
||||||
[RidwellPickup("Plastic Film", "offer_123", 1, "product_123", 1)],
|
EventState.INITIALIZED,
|
||||||
EventState.INITIALIZED,
|
)
|
||||||
)
|
|
||||||
]
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -80,8 +77,6 @@ async def mock_aioridwell_fixture(hass, client, config):
|
||||||
), patch(
|
), patch(
|
||||||
"homeassistant.components.ridwell.coordinator.async_get_client",
|
"homeassistant.components.ridwell.coordinator.async_get_client",
|
||||||
return_value=client,
|
return_value=client,
|
||||||
), freeze_time(
|
|
||||||
"2022-01-01"
|
|
||||||
):
|
):
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ async def test_entry_diagnostics(hass, config_entry, hass_client, setup_config_e
|
||||||
"_async_request": None,
|
"_async_request": None,
|
||||||
"event_id": "event_123",
|
"event_id": "event_123",
|
||||||
"pickup_date": {
|
"pickup_date": {
|
||||||
"__type": "<class 'freezegun.api.FakeDate'>",
|
"__type": "<class 'datetime.date'>",
|
||||||
"isoformat": "2022-01-24",
|
"isoformat": "2022-01-24",
|
||||||
},
|
},
|
||||||
"pickups": [
|
"pickups": [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue