Skip to content

Commit 4fa40c4

Browse files
WIP
1 parent 0f256a4 commit 4fa40c4

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

custom_components/label_state/binary_sensor.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
from homeassistant.config_entries import ConfigEntry
77
from homeassistant.const import (
88
CONF_NAME,
9+
CONF_UNIQUE_ID,
910
STATE_UNAVAILABLE,
1011
STATE_UNKNOWN,
1112
)
1213
from homeassistant.core import Event, EventStateChangedData, HomeAssistant, callback
1314
from homeassistant.helpers import entity_registry as er
1415
from homeassistant.helpers.entity_platform import (
1516
AddConfigEntryEntitiesCallback,
17+
AddEntitiesCallback,
1618
)
1719
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED
1820
from homeassistant.helpers.event import async_track_state_change_event
21+
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
1922

2023
from .const import (
2124
ATTR_ENTITIES,
@@ -69,6 +72,37 @@ async def async_setup_entry(
6972
)
7073

7174

75+
async def async_setup_platform(
76+
hass: HomeAssistant,
77+
config: ConfigType,
78+
async_add_entities: AddEntitiesCallback,
79+
discovery_info: DiscoveryInfoType | None = None,
80+
) -> None:
81+
"""Set up the min/max/mean sensor."""
82+
label: str = config[CONF_LABEL]
83+
name: str | None = config.get(CONF_NAME)
84+
state_type: str = config[CONF_STATE_TYPE]
85+
state_to: str | None = config.get(CONF_STATE_TO)
86+
state_lower_limit: float | None = config.get(CONF_STATE_LOWER_LIMIT)
87+
state_upper_limit: float | None = config.get(CONF_STATE_UPPER_LIMIT)
88+
unique_id = config.get(CONF_UNIQUE_ID)
89+
90+
async_add_entities(
91+
[
92+
LabelStateBinarySensor(
93+
hass,
94+
label,
95+
name,
96+
state_type,
97+
state_to,
98+
state_lower_limit,
99+
state_upper_limit,
100+
unique_id,
101+
)
102+
]
103+
)
104+
105+
72106
class LabelStateBinarySensor(BinarySensorEntity):
73107
"""Representation of a Label State sensor."""
74108

tests/test_binary_sensor.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
"""The test for the label_state binary sensor platform."""
22

33
import pytest
4+
from homeassistant.const import (
5+
CONF_ENTITY_ID,
6+
CONF_NAME,
7+
CONF_PLATFORM,
8+
CONF_UNIQUE_ID,
9+
EVENT_HOMEASSISTANT_STARTED,
10+
STATE_ON,
11+
)
412
from homeassistant.core import HomeAssistant
513
from homeassistant.helpers import entity_registry as er
614
from homeassistant.helpers import label_registry as lr
@@ -117,38 +125,37 @@ async def test_numeric_state_sensor(
117125
"""Test the numeric state sensor."""
118126

119127
test_label = label_registry.async_create(
120-
"test",
128+
"test_numeric_state",
121129
)
122130

123131
sensor1_entity_entry = entity_registry.async_get_or_create(
124132
"sensor", "test_1", "unique", suggested_object_id="test_1"
125133
)
126134
sensor1_entity_entry.labels.add(test_label.label_id)
135+
await hass.async_block_till_done()
127136
assert sensor1_entity_entry.entity_id == "sensor.test_1"
128137
assert test_label.label_id in sensor1_entity_entry.labels
129138

130139
sensor2_entity_entry = entity_registry.async_get_or_create(
131140
"sensor", "test_2", "unique", suggested_object_id="test_2"
132141
)
133142
sensor2_entity_entry.labels.add(test_label.label_id)
143+
await hass.async_block_till_done()
134144
assert sensor2_entity_entry.entity_id == "sensor.test_2"
145+
assert test_label.label_id in sensor2_entity_entry.labels
135146

136-
config = MockConfigEntry(
137-
domain="label_state",
138-
data={},
139-
options={
147+
config = {
148+
"binary_sensor": {
149+
"platform": "label_state",
140150
"name": "test_numeric_state",
141151
"label": test_label.label_id,
142152
"state_type": "numeric_state",
143153
"state_lower_limit": state_lower_limit,
144154
"state_upper_limit": state_upper_limit,
145-
},
146-
title="test_numeric_state",
147-
)
155+
}
156+
}
148157

149-
# config.add_to_hass(hass)
150-
await setup_integration(hass, config)
151-
# assert await hass.config_entries.async_setup(config.entry_id)
158+
assert await async_setup_component(hass, "binary_sensor", config)
152159
await hass.async_block_till_done()
153160

154161
hass.states.async_set(sensor1_entity_entry.entity_id, state_1)
@@ -157,6 +164,10 @@ async def test_numeric_state_sensor(
157164
hass.states.async_set(sensor2_entity_entry.entity_id, state_2)
158165
await hass.async_block_till_done()
159166

167+
state1 = hass.states.get(sensor1_entity_entry.entity_id)
168+
assert state1 is not None
169+
assert state1.state == state_1
170+
160171
state = hass.states.get("binary_sensor.test_numeric_state")
161172

162173
assert state is not None

0 commit comments

Comments
 (0)