mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Add binary sensors to Hydro-Quebec Peak Events (#185148)
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
"""Binary sensors for the Hydro-Québec Peak Events integration."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, timedelta
|
||||
from typing import override
|
||||
|
||||
from hydropeak_opendata import PeakEvent
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .coordinator import HydroQuebecPeakConfigEntry
|
||||
from .entity import HydroQuebecPeakEntity
|
||||
|
||||
# The coordinator handles all I/O; entities only read its data
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
def _peak_on_day(events: tuple[PeakEvent, ...], day: date, period: str) -> bool:
|
||||
"""Whether an event of the given period starts on the given local day."""
|
||||
return any(
|
||||
event.period == period and dt_util.as_local(event.start).date() == day
|
||||
for event in events
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class HydroQuebecPeakBinarySensorDescription(BinarySensorEntityDescription):
|
||||
"""Describes a Hydro-Québec peak event binary sensor."""
|
||||
|
||||
value_fn: Callable[[tuple[PeakEvent, ...]], bool]
|
||||
|
||||
|
||||
BINARY_SENSORS: tuple[HydroQuebecPeakBinarySensorDescription, ...] = (
|
||||
HydroQuebecPeakBinarySensorDescription(
|
||||
key="peak_active",
|
||||
translation_key="peak_active",
|
||||
value_fn=lambda events: any(
|
||||
event.is_active(dt_util.utcnow()) for event in events
|
||||
),
|
||||
),
|
||||
HydroQuebecPeakBinarySensorDescription(
|
||||
key="peak_today_am",
|
||||
translation_key="peak_today_am",
|
||||
value_fn=lambda events: _peak_on_day(events, dt_util.now().date(), "AM"),
|
||||
),
|
||||
HydroQuebecPeakBinarySensorDescription(
|
||||
key="peak_today_pm",
|
||||
translation_key="peak_today_pm",
|
||||
value_fn=lambda events: _peak_on_day(events, dt_util.now().date(), "PM"),
|
||||
),
|
||||
HydroQuebecPeakBinarySensorDescription(
|
||||
key="peak_tomorrow_am",
|
||||
translation_key="peak_tomorrow_am",
|
||||
value_fn=lambda events: _peak_on_day(
|
||||
events, dt_util.now().date() + timedelta(days=1), "AM"
|
||||
),
|
||||
),
|
||||
HydroQuebecPeakBinarySensorDescription(
|
||||
key="peak_tomorrow_pm",
|
||||
translation_key="peak_tomorrow_pm",
|
||||
value_fn=lambda events: _peak_on_day(
|
||||
events, dt_util.now().date() + timedelta(days=1), "PM"
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: HydroQuebecPeakConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the binary sensor platform."""
|
||||
async_add_entities(
|
||||
HydroQuebecPeakBinarySensor(entry.runtime_data, description)
|
||||
for description in BINARY_SENSORS
|
||||
)
|
||||
|
||||
|
||||
class HydroQuebecPeakBinarySensor(HydroQuebecPeakEntity, BinarySensorEntity):
|
||||
"""State of peak events for one offer."""
|
||||
|
||||
entity_description: HydroQuebecPeakBinarySensorDescription
|
||||
|
||||
@property
|
||||
@override
|
||||
def is_on(self) -> bool:
|
||||
"""Return the computed state."""
|
||||
return self.entity_description.value_fn(self.coordinator.data)
|
||||
@@ -16,7 +16,7 @@ CONF_OFFER: Final = "offer"
|
||||
SCAN_INTERVAL: Final = timedelta(minutes=15)
|
||||
BOUNDARY_REFRESH_DELAY: Final = timedelta(seconds=1)
|
||||
|
||||
PLATFORMS: Final = [Platform.SENSOR]
|
||||
PLATFORMS: Final = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||
|
||||
EVENTS_TABLE_URL: Final = (
|
||||
"https://donnees.hydroquebec.com/explore/dataset/evenements-pointe/table/"
|
||||
|
||||
@@ -16,6 +16,23 @@
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"peak_active": {
|
||||
"name": "Peak event in progress"
|
||||
},
|
||||
"peak_today_am": {
|
||||
"name": "Peak event today AM"
|
||||
},
|
||||
"peak_today_pm": {
|
||||
"name": "Peak event today PM"
|
||||
},
|
||||
"peak_tomorrow_am": {
|
||||
"name": "Peak event tomorrow AM"
|
||||
},
|
||||
"peak_tomorrow_pm": {
|
||||
"name": "Peak event tomorrow PM"
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
"event_begin": {
|
||||
"name": "Peak event begins"
|
||||
|
||||
@@ -0,0 +1,501 @@
|
||||
# serializer version: 1
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_in_progress-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_in_progress',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event in progress',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event in progress',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_active',
|
||||
'unique_id': 'CPC-D_peak_active',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_in_progress-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event in progress',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_in_progress',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_today_am-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_today_am',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event today AM',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event today AM',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_today_am',
|
||||
'unique_id': 'CPC-D_peak_today_am',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_today_am-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event today AM',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_today_am',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_today_pm-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_today_pm',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event today PM',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event today PM',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_today_pm',
|
||||
'unique_id': 'CPC-D_peak_today_pm',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_today_pm-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event today PM',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_today_pm',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_tomorrow_am-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_tomorrow_am',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event tomorrow AM',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event tomorrow AM',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_tomorrow_am',
|
||||
'unique_id': 'CPC-D_peak_tomorrow_am',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_tomorrow_am-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event tomorrow AM',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_tomorrow_am',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_tomorrow_pm-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_tomorrow_pm',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event tomorrow PM',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event tomorrow PM',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_tomorrow_pm',
|
||||
'unique_id': 'CPC-D_peak_tomorrow_pm',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T17:00:00+00:00][binary_sensor.cpc_d_peak_event_tomorrow_pm-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event tomorrow PM',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_tomorrow_pm',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_in_progress-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_in_progress',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event in progress',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event in progress',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_active',
|
||||
'unique_id': 'CPC-D_peak_active',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_in_progress-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event in progress',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_in_progress',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_today_am-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_today_am',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event today AM',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event today AM',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_today_am',
|
||||
'unique_id': 'CPC-D_peak_today_am',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_today_am-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event today AM',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_today_am',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_today_pm-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_today_pm',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event today PM',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event today PM',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_today_pm',
|
||||
'unique_id': 'CPC-D_peak_today_pm',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_today_pm-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event today PM',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_today_pm',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_tomorrow_am-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_tomorrow_am',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event tomorrow AM',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event tomorrow AM',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_tomorrow_am',
|
||||
'unique_id': 'CPC-D_peak_tomorrow_am',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_tomorrow_am-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event tomorrow AM',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_tomorrow_am',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_tomorrow_pm-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_tomorrow_pm',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Peak event tomorrow PM',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Peak event tomorrow PM',
|
||||
'platform': 'hydroquebec_peak',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'peak_tomorrow_pm',
|
||||
'unique_id': 'CPC-D_peak_tomorrow_pm',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[2026-01-09T22:00:00+00:00][binary_sensor.cpc_d_peak_event_tomorrow_pm-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'CPC-D Peak event tomorrow PM',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.cpc_d_peak_event_tomorrow_pm',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,94 @@
|
||||
"""Tests for the Hydro-Québec Peak Events binary sensors."""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
||||
|
||||
PEAK_ACTIVE = "binary_sensor.cpc_d_peak_event_in_progress"
|
||||
TODAY_PM = "binary_sensor.cpc_d_peak_event_today_pm"
|
||||
TOMORROW_AM = "binary_sensor.cpc_d_peak_event_tomorrow_am"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"now",
|
||||
[
|
||||
# 12:00 EST: AM and PM events today, AM event tomorrow
|
||||
"2026-01-09T17:00:00+00:00",
|
||||
# 17:00 EST: the evening event is in progress
|
||||
"2026-01-09T22:00:00+00:00",
|
||||
],
|
||||
)
|
||||
async def test_binary_sensors(
|
||||
hass: HomeAssistant,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_client: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
now: str,
|
||||
) -> None:
|
||||
"""Test the binary sensor entities before and during an event."""
|
||||
freezer.move_to(now)
|
||||
with patch(
|
||||
"homeassistant.components.hydroquebec_peak.PLATFORMS",
|
||||
[Platform.BINARY_SENSOR],
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
async def test_peak_active_flips_at_boundary(
|
||||
hass: HomeAssistant,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
mock_client: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""The in-progress flag flips shortly after the event start, not the next poll."""
|
||||
# 15:59 EST, one minute before the 16:00-20:00 EST event
|
||||
freezer.move_to("2026-01-09T20:59:00+00:00")
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
state = hass.states.get(PEAK_ACTIVE)
|
||||
assert state is not None
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
freezer.move_to("2026-01-09T21:00:02+00:00")
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(PEAK_ACTIVE)
|
||||
assert state is not None
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_day_flags_roll_over_at_midnight(
|
||||
hass: HomeAssistant,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
mock_client: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Day flags roll over at local midnight, not the next poll."""
|
||||
# 23:59 EST, the night before the 2026-01-10 AM event
|
||||
freezer.move_to("2026-01-10T04:59:00+00:00")
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert hass.states.get(TODAY_PM).state == STATE_ON
|
||||
assert hass.states.get(TOMORROW_AM).state == STATE_ON
|
||||
|
||||
freezer.move_to("2026-01-10T05:00:02+00:00")
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(TODAY_PM).state == STATE_OFF
|
||||
assert hass.states.get(TOMORROW_AM).state == STATE_OFF
|
||||
Reference in New Issue
Block a user