Skip to content

Commit ff59a4a

Browse files
Merge pull request #9 from andrew-codechimp/tests
Tests
2 parents fdc4c9e + fe0cb83 commit ff59a4a

22 files changed

+574
-13
lines changed

.github/workflows/pull.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
name: Pull actions
3+
4+
on:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
jobs:
9+
validate:
10+
runs-on: "ubuntu-latest"
11+
name: Validate
12+
steps:
13+
- uses: "actions/checkout@v4"
14+
15+
- name: HACS validation
16+
uses: "hacs/action@main"
17+
with:
18+
category: "integration"
19+
20+
- name: Hassfest validation
21+
uses: "home-assistant/actions/hassfest@master"
22+
23+
tests:
24+
runs-on: "ubuntu-latest"
25+
name: Run tests
26+
steps:
27+
- name: Check out code from GitHub
28+
uses: "actions/checkout@v4"
29+
- name: Setup Python
30+
uses: "actions/setup-python@v5"
31+
with:
32+
python-version: "3.13"
33+
- name: Install requirements
34+
run: python3 -m pip install -r requirements.txt -r requirements_test.txt
35+
- name: Run tests
36+
run: |
37+
pytest \
38+
-qq \
39+
--timeout=9 \
40+
--durations=10 \
41+
-n auto \
42+
--cov custom_components.periodic_min_max \
43+
-o console_output_style=count \
44+
-p no:sugar \
45+
tests

.github/workflows/push.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
name: Push actions
3+
4+
on:
5+
push:
6+
branches:
7+
- main
8+
workflow_dispatch:
9+
10+
jobs:
11+
tests:
12+
runs-on: "ubuntu-latest"
13+
name: Run tests
14+
steps:
15+
- name: Check out code from GitHub
16+
uses: "actions/checkout@v4"
17+
- name: Setup Python
18+
uses: "actions/setup-python@v5"
19+
with:
20+
python-version: "3.13"
21+
- name: Install requirements
22+
run: python3 -m pip install -r requirements.txt -r requirements_test.txt
23+
- name: Run tests
24+
run: |
25+
pytest \
26+
-qq \
27+
--timeout=9 \
28+
--durations=10 \
29+
-n auto \
30+
--cov custom_components.periodic_min_max \
31+
-o console_output_style=count \
32+
-p no:sugar \
33+
tests

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"type": "python",
1010
"request": "launch",
1111
"module": "homeassistant",
12-
"justMyCode": false,
12+
"justMyCode": true,
1313
"args": [
1414
"--debug",
1515
"-c",

.vscode/settings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"[python]": {
3-
"editor.defaultFormatter": "ms-python.black-formatter"
3+
"editor.defaultFormatter": "charliermarsh.ruff"
44
},
5-
"python.formatting.provider": "none"
5+
"terminal.integrated.env.linux": {
6+
"PYTHONPATH": "${workspaceRoot}"
7+
}
68
}

custom_components/periodic_min_max/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
from logging import Logger, getLogger
55
from pathlib import Path
6+
67
from homeassistant.const import Platform
78

89
LOGGER: Logger = getLogger(__package__)

custom_components/periodic_min_max/sensor.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ async def async_setup_platform(
9696

9797
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
9898

99-
async_add_entities([PeriodicMinMaxSensor(hass, entity_id, name, sensor_type, unique_id)])
99+
async_add_entities(
100+
[PeriodicMinMaxSensor(hass, entity_id, name, sensor_type, unique_id)]
101+
)
100102

101103

102104
class PeriodicMinMaxSensor(SensorEntity, RestoreEntity):
@@ -136,13 +138,14 @@ def __init__(
136138
self.max_value: float | None = None
137139
self._state: Any = None
138140

139-
140141
async def async_added_to_hass(self) -> None:
141142
"""Handle added to Hass."""
142143

143144
self.async_on_remove(
144145
async_track_state_change_event(
145-
self.hass, self._source_entity_id, self._async_min_max_sensor_state_listener
146+
self.hass,
147+
self._source_entity_id,
148+
self._async_min_max_sensor_state_listener,
146149
)
147150
)
148151

@@ -158,18 +161,34 @@ async def async_added_to_hass(self) -> None:
158161

159162
if entry:
160163
self._unit_of_measurement = entry.unit_of_measurement
161-
self._attr_device_class = SensorDeviceClass(entry.device_class) if entry.device_class else None
162-
self._attr_icon = entry.icon if entry.icon else entry.original_icon if entry.original_icon else ICON
164+
self._attr_device_class = (
165+
SensorDeviceClass(entry.device_class) if entry.device_class else None
166+
)
167+
self._attr_icon = (
168+
entry.icon
169+
if entry.icon
170+
else entry.original_icon
171+
if entry.original_icon
172+
else ICON
173+
)
163174

164175
state = await self.async_get_last_state()
165-
if state is not None and state.state not in [STATE_UNKNOWN, STATE_UNAVAILABLE]:
176+
if state is not None and state.state not in [
177+
STATE_UNKNOWN,
178+
STATE_UNAVAILABLE,
179+
]:
166180
self._state = float(state.state)
167181
self._calc_values()
168182

169183
# Replay current state of source entitiy
170184
state = self.hass.states.get(self._source_entity_id)
171185
state_event: Event[EventStateChangedData] = Event(
172-
"", {"entity_id": self._source_entity_id, "new_state": state, "old_state": None}
186+
"",
187+
{
188+
"entity_id": self._source_entity_id,
189+
"new_state": state,
190+
"old_state": None,
191+
},
173192
)
174193
self._async_min_max_sensor_state_listener(state_event, update_state=False)
175194

@@ -259,4 +278,4 @@ async def handle_reset(self) -> None:
259278
self.min_value = self._state
260279
self.max_value = self._state
261280

262-
self.async_write_ha_state()
281+
self.async_write_ha_state()

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,7 @@ warn_unused_ignores = true
129129

130130
[tool.ruff.lint.mccabe]
131131
max-complexity = 25
132+
133+
[tool.pytest.ini_options]
134+
testpaths = ["tests"]
135+
asyncio_mode = "auto"

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
asyncio_mode = auto

requirements_dev.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
homeassistant
2+
pre-commit
3+
mypy

requirements_test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest-homeassistant-custom-component

0 commit comments

Comments
 (0)