diff --git a/homeassistant/components/teslemetry/__init__.py b/homeassistant/components/teslemetry/__init__.py index af4ce26a0cc2ae..af59dc7cdfdd91 100644 --- a/homeassistant/components/teslemetry/__init__.py +++ b/homeassistant/components/teslemetry/__init__.py @@ -37,6 +37,7 @@ PLATFORMS: Final = [ Platform.BINARY_SENSOR, Platform.BUTTON, + Platform.CALENDAR, Platform.CLIMATE, Platform.COVER, Platform.DEVICE_TRACKER, diff --git a/homeassistant/components/teslemetry/calendar.py b/homeassistant/components/teslemetry/calendar.py new file mode 100644 index 00000000000000..1deb4cb5a296fd --- /dev/null +++ b/homeassistant/components/teslemetry/calendar.py @@ -0,0 +1,288 @@ +"""Calendar platform for Teslemetry integration.""" + +from __future__ import annotations + +from datetime import datetime, timedelta +from typing import Any + +from attr import dataclass + +from homeassistant.components.calendar import CalendarEntity, CalendarEvent +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from homeassistant.util import dt as dt_util + +from . import TeslemetryConfigEntry +from .entity import TeslemetryEnergyInfoEntity + + +async def async_setup_entry( + hass: HomeAssistant, + entry: TeslemetryConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the Teslemetry Calendar platform from a config entry.""" + + entities_to_add: list[CalendarEntity] = [] + + # Add buy tariff calendar entities + entities_to_add.extend( + TeslemetryTariffSchedule(energy, "tariff_content_v2") + for energy in entry.runtime_data.energysites + if energy.info_coordinator.data.get("tariff_content_v2_seasons") + ) + + # Add sell tariff calendar entities + entities_to_add.extend( + TeslemetryTariffSchedule(energy, "tariff_content_v2_sell_tariff") + for energy in entry.runtime_data.energysites + if energy.info_coordinator.data.get("tariff_content_v2_sell_tariff_seasons") + ) + + async_add_entities(entities_to_add) + + +@dataclass +class TariffPeriod: + """A single tariff period.""" + + name: str + price: float + from_hour: int = 0 + from_minute: int = 0 + to_hour: int = 0 + to_minute: int = 0 + + +class TeslemetryTariffSchedule(TeslemetryEnergyInfoEntity, CalendarEntity): + """Energy Site Tariff Schedule Calendar.""" + + def __init__( + self, + data: Any, + key_base: str, + ) -> None: + """Initialize the tariff schedule calendar.""" + self.key_base: str = key_base + self.seasons: dict[str, dict[str, Any]] = {} + self.charges: dict[str, dict[str, Any]] = {} + super().__init__(data, key_base) + + @property + def event(self) -> CalendarEvent | None: + """Return the current active tariff event.""" + now = dt_util.now() + current_season_name = self._get_current_season(now) + + if not current_season_name or not self.seasons.get(current_season_name): + return None + + # Get the time of use periods for the current season + tou_periods = self.seasons[current_season_name].get("tou_periods", {}) + + for period_name, period_group in tou_periods.items(): + for period_def in period_group.get("periods", []): + # Check if today is within the period's day of week range + day_of_week = now.weekday() # Monday is 0, Sunday is 6 + from_day = period_def.get("fromDayOfWeek", 0) + to_day = period_def.get("toDayOfWeek", 6) + if from_day > day_of_week > to_day: + continue + + # Calculate start and end times for today + from_hour = period_def.get("fromHour", 0) % 24 + from_minute = period_def.get("fromMinute", 0) % 60 + to_hour = period_def.get("toHour", 0) % 24 + to_minute = period_def.get("toMinute", 0) % 60 + + start_time = now.replace( + hour=from_hour, minute=from_minute, second=0, microsecond=0 + ) + end_time = now.replace( + hour=to_hour, minute=to_minute, second=0, microsecond=0 + ) + + # Handle periods that cross midnight + if end_time <= start_time: + potential_end_time = end_time + timedelta(days=1) + if start_time <= now < potential_end_time: + end_time = potential_end_time + elif (start_time - timedelta(days=1)) <= now < end_time: + start_time -= timedelta(days=1) + else: + continue + elif not (start_time <= now < end_time): + continue + + # Create calendar event for the active period + price = self._get_price_for_period(current_season_name, period_name) + price_str = f"{price:.2f}/kWh" if price is not None else "Unknown Price" + + return CalendarEvent( + start=start_time, + end=end_time, + summary=f"{period_name.capitalize().replace('_', ' ')}: {price_str}", + description=( + f"Season: {current_season_name.capitalize()}\n" + f"Period: {period_name.capitalize().replace('_', ' ')}\n" + f"Price: {price_str}" + ), + uid=f"{self.key_base}_{current_season_name}_{period_name}_{start_time.isoformat()}", + ) + + return None # No active period found for the current time and season + + async def async_get_events( + self, + hass: HomeAssistant, + start_date: datetime, + end_date: datetime, + ) -> list[CalendarEvent]: + """Return calendar events (tariff periods) within a datetime range.""" + events: list[CalendarEvent] = [] + + # Convert dates to local timezone + start_date = dt_util.as_local(start_date) + end_date = dt_util.as_local(end_date) + + # Process each day in the requested range + current_day = dt_util.start_of_local_day(start_date) + while current_day < end_date: + season_name = self._get_current_season(current_day) + if not season_name or not self.seasons.get(season_name): + current_day += timedelta(days=1) + continue + + # Get the time of use periods for the season + tou_periods = self.seasons[season_name].get("tou_periods", {}) + day_of_week = current_day.weekday() + + for period_name, period_group in tou_periods.items(): + for period_def in period_group.get("periods", []): + # Check if current day falls within the period's day range + from_day = period_def.get("fromDayOfWeek", 0) + to_day = period_def.get("toDayOfWeek", 6) + if from_day > day_of_week > to_day: + continue + + # Extract period timing for current day + from_hour = period_def.get("fromHour", 0) % 24 + from_minute = period_def.get("fromMinute", 0) % 60 + to_hour = period_def.get("toHour", 0) % 24 + to_minute = period_def.get("toMinute", 0) % 60 + + start_time = current_day.replace( + hour=from_hour, minute=from_minute, second=0, microsecond=0 + ) + end_time = current_day.replace( + hour=to_hour, minute=to_minute, second=0, microsecond=0 + ) + + # Adjust for periods crossing midnight + if end_time <= start_time: + end_time += timedelta(days=1) + + # Check for overlap with requested date range + if start_time < end_date and end_time > start_date: + price = self._get_price_for_period(season_name, period_name) + price_str = ( + f"{price:.2f}/kWh" if price is not None else "Unknown Price" + ) + events.append( + CalendarEvent( + start=start_time, + end=end_time, + summary=f"{period_name.capitalize().replace('_', ' ')}: {price_str}", + description=( + f"Season: {season_name.capitalize()}\n" + f"Period: {period_name.capitalize().replace('_', ' ')}\n" + f"Price: {price_str}" + ), + uid=f"{self.key_base}_{season_name}_{period_name}_{start_time.isoformat()}", + ) + ) + + current_day += timedelta(days=1) + + # Sort events chronologically + events.sort(key=lambda x: x.start) + return events + + def _get_current_season(self, date_to_check: datetime) -> str | None: + """Determine the active season for a given date.""" + local_date = dt_util.as_local(date_to_check) + year = local_date.year + + for season_name, season_data in self.seasons.items(): + if not season_data: + continue + + # Extract season date boundaries + try: + from_month = season_data["fromMonth"] + from_day = season_data["fromDay"] + to_month = season_data["toMonth"] + to_day = season_data["toDay"] + + # Handle seasons that cross year boundaries + start_year = year + end_year = year + + # Determine if season crosses year boundary + if from_month > to_month or ( + from_month == to_month and from_day > to_day + ): + if local_date.month > from_month or ( + local_date.month == from_month and local_date.day >= from_day + ): + end_year = year + 1 + else: + start_year = year - 1 + + season_start = local_date.replace( + year=start_year, + month=from_month, + day=from_day, + hour=0, + minute=0, + second=0, + microsecond=0, + ) + # Create exclusive end date + season_end = local_date.replace( + year=end_year, + month=to_month, + day=to_day, + hour=0, + minute=0, + second=0, + microsecond=0, + ) + timedelta(days=1) + + if season_start <= local_date < season_end: + return season_name + except (KeyError, ValueError): + continue + + return None # No matching season found + + def _get_price_for_period(self, season_name: str, period_name: str) -> float | None: + """Get the price for a specific season and period name.""" + try: + # Get rates for the season with fallback to "ALL" + season_charges = self.charges.get(season_name, self.charges.get("ALL", {})) + rates = season_charges.get("rates", {}) + # Get price for the period with fallback to "ALL" + price = rates.get(period_name, rates.get("ALL")) + return float(price) if price is not None else None + except (KeyError, ValueError, TypeError): + return None + + def _async_update_attrs(self) -> None: + """Update the Calendar attributes from coordinator data.""" + # Load tariff data from coordinator + self.seasons = self.coordinator.data.get(f"{self.key_base}_seasons", {}) + self.charges = self.coordinator.data.get(f"{self.key_base}_energy_charges", {}) + + # Set availability based on data presence + self._attr_available = bool(self.seasons and self.charges) diff --git a/homeassistant/components/teslemetry/coordinator.py b/homeassistant/components/teslemetry/coordinator.py index eed00ebc64f802..a9ad6da651ba3d 100644 --- a/homeassistant/components/teslemetry/coordinator.py +++ b/homeassistant/components/teslemetry/coordinator.py @@ -77,7 +77,10 @@ async def _async_update_data(self) -> dict[str, Any]: except TeslaFleetError as e: raise UpdateFailed(e.message) from e - return flatten(data) + return flatten( + data, + skip_keys=["daily_charges", "demand_charges", "energy_charges", "seasons"], + ) class TeslemetryEnergySiteLiveCoordinator(DataUpdateCoordinator[dict[str, Any]]): @@ -160,7 +163,10 @@ async def _async_update_data(self) -> dict[str, Any]: except TeslaFleetError as e: raise UpdateFailed(e.message) from e - return flatten(data) + return flatten( + data, + skip_keys=["daily_charges", "demand_charges", "energy_charges", "seasons"], + ) class TeslemetryEnergyHistoryCoordinator(DataUpdateCoordinator[dict[str, Any]]): diff --git a/homeassistant/components/teslemetry/helpers.py b/homeassistant/components/teslemetry/helpers.py index c6f15d7bfdf707..4c29d5cf27c982 100644 --- a/homeassistant/components/teslemetry/helpers.py +++ b/homeassistant/components/teslemetry/helpers.py @@ -9,14 +9,17 @@ from .const import DOMAIN, LOGGER -def flatten(data: dict[str, Any], parent: str | None = None) -> dict[str, Any]: +def flatten( + data: dict[str, Any], parent: str | None = None, skip_keys: list[str] | None = None +) -> dict[str, Any]: """Flatten the data structure.""" result = {} for key, value in data.items(): + skip = skip_keys and key in skip_keys if parent: key = f"{parent}_{key}" - if isinstance(value, dict): - result.update(flatten(value, key)) + if isinstance(value, dict) and not skip: + result.update(flatten(value, key, skip_keys)) else: result[key] = value return result diff --git a/homeassistant/components/teslemetry/strings.json b/homeassistant/components/teslemetry/strings.json index b78f2d00f60ffb..47f83cebf84398 100644 --- a/homeassistant/components/teslemetry/strings.json +++ b/homeassistant/components/teslemetry/strings.json @@ -274,6 +274,14 @@ "name": "Wake" } }, + "calendar": { + "tariff_content_v2": { + "name": "Buy tariff" + }, + "tariff_content_v2_sell_tariff": { + "name": "Sell tariff" + } + }, "climate": { "climate_state_cabin_overheat_protection": { "name": "Cabin overheat protection" diff --git a/tests/components/teslemetry/fixtures/site_info.json b/tests/components/teslemetry/fixtures/site_info.json index 60958bbabbbd09..adbcff69392db7 100644 --- a/tests/components/teslemetry/fixtures/site_info.json +++ b/tests/components/teslemetry/fixtures/site_info.json @@ -122,6 +122,130 @@ "installation_time_zone": "", "max_site_meter_power_ac": 1000000000, "min_site_meter_power_ac": -1000000000, + "tariff_content_v2": { + "code": "Test", + "name": "Battery Maximiser", + "utility": "Origin", + "daily_charges": [ + { + "name": "Charge" + } + ], + "demand_charges": { + "ALL": { + "rates": { + "ALL": 0 + } + }, + "Summer": {}, + "Winter": {} + }, + "energy_charges": { + "ALL": { + "rates": { + "ALL": 0 + } + }, + "Summer": { + "rates": { + "OFF_PEAK": 0.198, + "ON_PEAK": 0.22 + } + }, + "Winter": {} + }, + "seasons": { + "Summer": { + "fromDay": 1, + "toDay": 31, + "fromMonth": 1, + "toMonth": 12, + "tou_periods": { + "OFF_PEAK": { + "periods": [ + { + "toDayOfWeek": 6, + "fromHour": 21, + "toHour": 16 + } + ] + }, + "ON_PEAK": { + "periods": [ + { + "toDayOfWeek": 6, + "fromHour": 16, + "toHour": 21 + } + ] + } + } + }, + "Winter": {} + }, + "sell_tariff": { + "name": "Battery Maximiser", + "utility": "Origin", + "daily_charges": [ + { + "name": "Charge" + } + ], + "demand_charges": { + "ALL": { + "rates": { + "ALL": 0 + } + }, + "Summer": {}, + "Winter": {} + }, + "energy_charges": { + "ALL": { + "rates": { + "ALL": 0 + } + }, + "Summer": { + "rates": { + "OFF_PEAK": 0.08, + "ON_PEAK": 0.16 + } + }, + "Winter": {} + }, + "seasons": { + "Summer": { + "fromDay": 1, + "toDay": 31, + "fromMonth": 1, + "toMonth": 12, + "tou_periods": { + "OFF_PEAK": { + "periods": [ + { + "toDayOfWeek": 6, + "fromHour": 21, + "toHour": 16 + } + ] + }, + "ON_PEAK": { + "periods": [ + { + "toDayOfWeek": 6, + "fromHour": 16, + "toHour": 21 + } + ] + } + } + }, + "Winter": {} + } + }, + "version": 1 + }, "vpp_backup_reserve_percent": 0 } } diff --git a/tests/components/teslemetry/snapshots/test_calendar.ambr b/tests/components/teslemetry/snapshots/test_calendar.ambr new file mode 100644 index 00000000000000..9c25c973101ea3 --- /dev/null +++ b/tests/components/teslemetry/snapshots/test_calendar.ambr @@ -0,0 +1,373 @@ +# serializer version: 1 +# name: test_calendar[calendar.energy_site_buy_tariff-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'calendar', + 'entity_category': None, + 'entity_id': 'calendar.energy_site_buy_tariff', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Buy tariff', + 'platform': 'teslemetry', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'tariff_content_v2', + 'unique_id': '123456-tariff_content_v2', + 'unit_of_measurement': None, + }) +# --- +# name: test_calendar[calendar.energy_site_buy_tariff-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'all_day': False, + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.20/kWh + ''', + 'end_time': '2024-01-01 16:00:00', + 'friendly_name': 'Energy Site Buy tariff', + 'location': '', + 'message': 'Off peak: 0.20/kWh', + 'start_time': '2023-12-31 21:00:00', + }), + 'context': , + 'entity_id': 'calendar.energy_site_buy_tariff', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_calendar[calendar.energy_site_sell_tariff-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'calendar', + 'entity_category': None, + 'entity_id': 'calendar.energy_site_sell_tariff', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Sell tariff', + 'platform': 'teslemetry', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'tariff_content_v2_sell_tariff', + 'unique_id': '123456-tariff_content_v2_sell_tariff', + 'unit_of_measurement': None, + }) +# --- +# name: test_calendar[calendar.energy_site_sell_tariff-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'all_day': False, + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.08/kWh + ''', + 'end_time': '2024-01-01 16:00:00', + 'friendly_name': 'Energy Site Sell tariff', + 'location': '', + 'message': 'Off peak: 0.08/kWh', + 'start_time': '2023-12-31 21:00:00', + }), + 'context': , + 'entity_id': 'calendar.energy_site_sell_tariff', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_calendar_events[calendar.energy_site_buy_tariff] + dict({ + 'calendar.energy_site_buy_tariff': dict({ + 'events': list([ + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.22/kWh + ''', + 'end': '2023-12-31T21:00:00-08:00', + 'start': '2023-12-31T16:00:00-08:00', + 'summary': 'On peak: 0.22/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.20/kWh + ''', + 'end': '2024-01-01T16:00:00-08:00', + 'start': '2023-12-31T21:00:00-08:00', + 'summary': 'Off peak: 0.20/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.22/kWh + ''', + 'end': '2024-01-01T21:00:00-08:00', + 'start': '2024-01-01T16:00:00-08:00', + 'summary': 'On peak: 0.22/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.20/kWh + ''', + 'end': '2024-01-02T16:00:00-08:00', + 'start': '2024-01-01T21:00:00-08:00', + 'summary': 'Off peak: 0.20/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.22/kWh + ''', + 'end': '2024-01-02T21:00:00-08:00', + 'start': '2024-01-02T16:00:00-08:00', + 'summary': 'On peak: 0.22/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.20/kWh + ''', + 'end': '2024-01-03T16:00:00-08:00', + 'start': '2024-01-02T21:00:00-08:00', + 'summary': 'Off peak: 0.20/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.22/kWh + ''', + 'end': '2024-01-03T21:00:00-08:00', + 'start': '2024-01-03T16:00:00-08:00', + 'summary': 'On peak: 0.22/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.20/kWh + ''', + 'end': '2024-01-04T16:00:00-08:00', + 'start': '2024-01-03T21:00:00-08:00', + 'summary': 'Off peak: 0.20/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.22/kWh + ''', + 'end': '2024-01-04T21:00:00-08:00', + 'start': '2024-01-04T16:00:00-08:00', + 'summary': 'On peak: 0.22/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.20/kWh + ''', + 'end': '2024-01-05T16:00:00-08:00', + 'start': '2024-01-04T21:00:00-08:00', + 'summary': 'Off peak: 0.20/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.22/kWh + ''', + 'end': '2024-01-05T21:00:00-08:00', + 'start': '2024-01-05T16:00:00-08:00', + 'summary': 'On peak: 0.22/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.20/kWh + ''', + 'end': '2024-01-06T16:00:00-08:00', + 'start': '2024-01-05T21:00:00-08:00', + 'summary': 'Off peak: 0.20/kWh', + }), + ]), + }), + }) +# --- +# name: test_calendar_events[calendar.energy_site_sell_tariff] + dict({ + 'calendar.energy_site_sell_tariff': dict({ + 'events': list([ + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.16/kWh + ''', + 'end': '2023-12-31T21:00:00-08:00', + 'start': '2023-12-31T16:00:00-08:00', + 'summary': 'On peak: 0.16/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.08/kWh + ''', + 'end': '2024-01-01T16:00:00-08:00', + 'start': '2023-12-31T21:00:00-08:00', + 'summary': 'Off peak: 0.08/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.16/kWh + ''', + 'end': '2024-01-01T21:00:00-08:00', + 'start': '2024-01-01T16:00:00-08:00', + 'summary': 'On peak: 0.16/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.08/kWh + ''', + 'end': '2024-01-02T16:00:00-08:00', + 'start': '2024-01-01T21:00:00-08:00', + 'summary': 'Off peak: 0.08/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.16/kWh + ''', + 'end': '2024-01-02T21:00:00-08:00', + 'start': '2024-01-02T16:00:00-08:00', + 'summary': 'On peak: 0.16/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.08/kWh + ''', + 'end': '2024-01-03T16:00:00-08:00', + 'start': '2024-01-02T21:00:00-08:00', + 'summary': 'Off peak: 0.08/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.16/kWh + ''', + 'end': '2024-01-03T21:00:00-08:00', + 'start': '2024-01-03T16:00:00-08:00', + 'summary': 'On peak: 0.16/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.08/kWh + ''', + 'end': '2024-01-04T16:00:00-08:00', + 'start': '2024-01-03T21:00:00-08:00', + 'summary': 'Off peak: 0.08/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.16/kWh + ''', + 'end': '2024-01-04T21:00:00-08:00', + 'start': '2024-01-04T16:00:00-08:00', + 'summary': 'On peak: 0.16/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.08/kWh + ''', + 'end': '2024-01-05T16:00:00-08:00', + 'start': '2024-01-04T21:00:00-08:00', + 'summary': 'Off peak: 0.08/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: On peak + Price: 0.16/kWh + ''', + 'end': '2024-01-05T21:00:00-08:00', + 'start': '2024-01-05T16:00:00-08:00', + 'summary': 'On peak: 0.16/kWh', + }), + dict({ + 'description': ''' + Season: Summer + Period: Off peak + Price: 0.08/kWh + ''', + 'end': '2024-01-06T16:00:00-08:00', + 'start': '2024-01-05T21:00:00-08:00', + 'summary': 'Off peak: 0.08/kWh', + }), + ]), + }), + }) +# --- diff --git a/tests/components/teslemetry/snapshots/test_diagnostics.ambr b/tests/components/teslemetry/snapshots/test_diagnostics.ambr index 6b02b2f6d83c8a..1c2df3b567f4f2 100644 --- a/tests/components/teslemetry/snapshots/test_diagnostics.ambr +++ b/tests/components/teslemetry/snapshots/test_diagnostics.ambr @@ -120,6 +120,134 @@ 'nameplate_energy': 40500, 'nameplate_power': 15000, 'site_name': 'Site', + 'tariff_content_v2_code': 'Test', + 'tariff_content_v2_daily_charges': list([ + dict({ + 'name': 'Charge', + }), + ]), + 'tariff_content_v2_demand_charges': dict({ + 'ALL': dict({ + 'rates': dict({ + 'ALL': 0, + }), + }), + 'Summer': dict({ + }), + 'Winter': dict({ + }), + }), + 'tariff_content_v2_energy_charges': dict({ + 'ALL': dict({ + 'rates': dict({ + 'ALL': 0, + }), + }), + 'Summer': dict({ + 'rates': dict({ + 'OFF_PEAK': 0.198, + 'ON_PEAK': 0.22, + }), + }), + 'Winter': dict({ + }), + }), + 'tariff_content_v2_name': 'Battery Maximiser', + 'tariff_content_v2_seasons': dict({ + 'Summer': dict({ + 'fromDay': 1, + 'fromMonth': 1, + 'toDay': 31, + 'toMonth': 12, + 'tou_periods': dict({ + 'OFF_PEAK': dict({ + 'periods': list([ + dict({ + 'fromHour': 21, + 'toDayOfWeek': 6, + 'toHour': 16, + }), + ]), + }), + 'ON_PEAK': dict({ + 'periods': list([ + dict({ + 'fromHour': 16, + 'toDayOfWeek': 6, + 'toHour': 21, + }), + ]), + }), + }), + }), + 'Winter': dict({ + }), + }), + 'tariff_content_v2_sell_tariff_daily_charges': list([ + dict({ + 'name': 'Charge', + }), + ]), + 'tariff_content_v2_sell_tariff_demand_charges': dict({ + 'ALL': dict({ + 'rates': dict({ + 'ALL': 0, + }), + }), + 'Summer': dict({ + }), + 'Winter': dict({ + }), + }), + 'tariff_content_v2_sell_tariff_energy_charges': dict({ + 'ALL': dict({ + 'rates': dict({ + 'ALL': 0, + }), + }), + 'Summer': dict({ + 'rates': dict({ + 'OFF_PEAK': 0.08, + 'ON_PEAK': 0.16, + }), + }), + 'Winter': dict({ + }), + }), + 'tariff_content_v2_sell_tariff_name': 'Battery Maximiser', + 'tariff_content_v2_sell_tariff_seasons': dict({ + 'Summer': dict({ + 'fromDay': 1, + 'fromMonth': 1, + 'toDay': 31, + 'toMonth': 12, + 'tou_periods': dict({ + 'OFF_PEAK': dict({ + 'periods': list([ + dict({ + 'fromHour': 21, + 'toDayOfWeek': 6, + 'toHour': 16, + }), + ]), + }), + 'ON_PEAK': dict({ + 'periods': list([ + dict({ + 'fromHour': 16, + 'toDayOfWeek': 6, + 'toHour': 21, + }), + ]), + }), + }), + }), + 'Winter': dict({ + }), + }), + 'tariff_content_v2_sell_tariff_utility': 'Origin', + 'tariff_content_v2_utility': 'Origin', + 'tariff_content_v2_version': 1, 'tou_settings_optimization_strategy': 'economics', 'tou_settings_schedule': list([ dict({ diff --git a/tests/components/teslemetry/test_calendar.py b/tests/components/teslemetry/test_calendar.py new file mode 100644 index 00000000000000..5370dc78244daf --- /dev/null +++ b/tests/components/teslemetry/test_calendar.py @@ -0,0 +1,75 @@ +"""Test the Teslemetry calendar platform.""" + +from datetime import datetime +from unittest.mock import AsyncMock + +from freezegun.api import FrozenDateTimeFactory +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.calendar import ( + DOMAIN as CALENDAR_DOMAIN, + EVENT_END_DATETIME, + EVENT_START_DATETIME, + SERVICE_GET_EVENTS, +) +from homeassistant.const import ATTR_ENTITY_ID, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er +from homeassistant.util import dt as dt_util + +from . import assert_entities, setup_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_calendar( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + freezer: FrozenDateTimeFactory, + mock_legacy: AsyncMock, +) -> None: + """Tests that the calendar entity is correct.""" + + TZ = dt_util.get_default_time_zone() + freezer.move_to(datetime(2024, 1, 1, 10, 0, 0, tzinfo=TZ)) + + entry = await setup_platform(hass, [Platform.CALENDAR]) + + assert_entities(hass, entry.entry_id, entity_registry, snapshot) + + +@pytest.mark.parametrize( + "entity_id", + [ + "calendar.energy_site_buy_tariff", + "calendar.energy_site_sell_tariff", + ], +) +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_calendar_events( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + freezer: FrozenDateTimeFactory, + mock_legacy: AsyncMock, + entity_id: str, +) -> None: + """Tests that the energy tariff calendar entity events are correct.""" + + TZ = dt_util.get_default_time_zone() + freezer.move_to(datetime(2024, 1, 1, 10, 0, 0, tzinfo=TZ)) + + await setup_platform(hass, [Platform.CALENDAR]) + result = await hass.services.async_call( + CALENDAR_DOMAIN, + SERVICE_GET_EVENTS, + { + ATTR_ENTITY_ID: [entity_id], + EVENT_START_DATETIME: dt_util.parse_datetime("2024-01-01T00:00:00Z"), + EVENT_END_DATETIME: dt_util.parse_datetime("2024-01-07T00:00:00Z"), + }, + blocking=True, + return_response=True, + ) + assert result == snapshot()