-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Expand file tree
/
Copy pathbinary_sensor.py
More file actions
86 lines (65 loc) · 2.54 KB
/
binary_sensor.py
File metadata and controls
86 lines (65 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Binary sensor platform for Nord Pool integration."""
from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.components.sensor import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import NordPoolConfigEntry
from .const import CONF_AREAS
from .coordinator import NordPoolDataUpdateCoordinator
from .entity import NordpoolBaseEntity
PARALLEL_UPDATES = 0
def get_tomorrow_price_available(
entity: NordpoolPriceBinarySensor,
) -> bool:
"""Return tomorrow price availability.
Output: True or False
"""
data = entity.coordinator.get_data_tomorrow()
if data and data.entries and entity.area in data.entries[0].entry:
return True
return False
@dataclass(frozen=True, kw_only=True)
class NordpoolBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Nord Pool default sensor entity."""
value_fn: Callable[[NordpoolPriceBinarySensor], bool | None]
BINARY_SENSOR_TYPES: tuple[NordpoolBinarySensorEntityDescription, ...] = (
NordpoolBinarySensorEntityDescription(
key="tomorrow_price_available",
translation_key="tomorrow_price_available",
value_fn=get_tomorrow_price_available,
entity_category=EntityCategory.DIAGNOSTIC,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: NordPoolConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Nord Pool sensor platform."""
coordinator = entry.runtime_data
areas = coordinator.config_entry.data[CONF_AREAS]
async_add_entities(
NordpoolPriceBinarySensor(coordinator, description, area)
for description in BINARY_SENSOR_TYPES
for area in areas
)
class NordpoolPriceBinarySensor(NordpoolBaseEntity, BinarySensorEntity):
"""Representation of a Nord Pool binary sensor."""
entity_description: NordpoolBinarySensorEntityDescription
def __init__(
self,
coordinator: NordPoolDataUpdateCoordinator,
entity_description: NordpoolBinarySensorEntityDescription,
area: str,
) -> None:
"""Initiate Nord Pool binary sensor."""
super().__init__(coordinator, entity_description, area)
@property
def is_on(self) -> bool | None:
"""Return true if the binary sensor is on."""
return self.entity_description.value_fn(self)