Skip to content

Commit

Permalink
fix: use async_forward_entry_setups for HA 2024.7.x+ (#79)
Browse files Browse the repository at this point in the history
* fix: use `async_forward_entry_setups` for HA 2024.7.x+

* update tests

* more test fixing

* use tox for tests

* remove old tests

* formatting

* fix tests

* formatting

* formatting and linting

* fix errors
  • Loading branch information
firstof9 authored Jun 27, 2024
1 parent bd795e9 commit bb824a5
Show file tree
Hide file tree
Showing 19 changed files with 280 additions and 200 deletions.
58 changes: 0 additions & 58 deletions .github/workflows/pull.yml

This file was deleted.

61 changes: 0 additions & 61 deletions .github/workflows/push.yml

This file was deleted.

86 changes: 86 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: Pull actions

on:
pull_request:
push:
branches:
- master

jobs:
validate:
runs-on: "ubuntu-latest"
name: Validate
steps:
- name: 📥 Checkout the repository
uses: actions/checkout@v4

- name: HACS validation
uses: "hacs/action@main"
with:
category: "integration"
ignore: brands

- name: Hassfest validation
uses: "home-assistant/actions/hassfest@master"

style:
runs-on: "ubuntu-latest"
name: Check style formatting
steps:
- name: 📥 Checkout the repository
uses: actions/checkout@v4
- name: 🛠️ Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
- run: python3 -m pip install black
- run: black .

tests:
runs-on: "ubuntu-latest"
name: Run tests
strategy:
matrix:
python-version:
# - "3.10"
# - "3.11"
- "3.12"

steps:
- name: 📥 Checkout the repository
uses: actions/checkout@v4
- name: 🛠️ Set up Python
uses: actions/setup-python@v5
with:
fetch-depth: 2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: 📦 Install requirements
run: |
pip install tox tox-gh-actions
- name: 🏃 Test with tox
run: tox
- name: 📤 Upload coverage to Codecov
uses: "actions/upload-artifact@v4"
with:
name: coverage-data
path: "coverage.xml"

coverage:
runs-on: ubuntu-latest
needs: tests
steps:
- name: 📥 Checkout the repository
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: 📥 Download coverage data
uses: actions/download-artifact@v4
with:
name: coverage-data
- name: 📤 Upload coverage report
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }} # required
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
pythonenv*
.coverage
coverage.xml
22 changes: 10 additions & 12 deletions custom_components/openei/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Custom integration to integrate OpenEI with Home Assistant."""
from datetime import datetime, timedelta

import logging
from datetime import datetime, timedelta

import openeihttp
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
import openeihttp

from .const import (
BINARY_SENSORS,
Expand All @@ -26,7 +27,9 @@
_LOGGER: logging.Logger = logging.getLogger(__package__)


async def async_setup(hass: HomeAssistant, config: Config):
async def async_setup( # pylint: disable-next=unused-argument
hass: HomeAssistant, config: Config
):
"""Set up this integration using YAML is not supported."""
return True

Expand Down Expand Up @@ -67,11 +70,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
raise ConfigEntryNotReady

hass.data[DOMAIN][entry.entry_id] = coordinator
for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True


Expand Down Expand Up @@ -110,7 +109,7 @@ async def _async_update_data(self) -> dict:
raise UpdateFailed() from exception
return self._data

async def _async_refresh_data(self, data=None) -> None:
async def _async_refresh_data(self) -> None:
"""Update data via library."""
delta = timedelta(hours=1)
now = datetime.now()
Expand Down Expand Up @@ -157,7 +156,7 @@ def get_sensors(hass, config) -> dict:
rate.update()
data = {}

for sensor in SENSOR_TYPES:
for sensor in SENSOR_TYPES: # pylint: disable=consider-using-dict-items
_sensor = {}
value = getattr(rate, SENSOR_TYPES[sensor].key)
if isinstance(value, tuple):
Expand All @@ -167,7 +166,7 @@ def get_sensors(hass, config) -> dict:
_sensor[sensor] = getattr(rate, SENSOR_TYPES[sensor].key)
data.update(_sensor)

for sensor in BINARY_SENSORS:
for sensor in BINARY_SENSORS: # pylint: disable=consider-using-dict-items
_sensor = {}
_sensor[sensor] = getattr(rate, sensor)
data.update(_sensor)
Expand All @@ -185,7 +184,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Update listener."""

if not config_entry.options:
return

Expand Down
18 changes: 11 additions & 7 deletions custom_components/openei/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Binary sensor platform for OpenEI."""
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorEntityDescription

from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand All @@ -11,12 +14,14 @@


async def async_setup_entry(hass, entry, async_add_devices):
"""Setup binary_sensor platform."""
"""Set up binary_sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]

binary_sensors = []
for binary_sensor in BINARY_SENSORS:
binary_sensors.append(OpenEIBinarySensor(hass, BINARY_SENSORS[binary_sensor], entry, coordinator))
for binary_sensor in BINARY_SENSORS: # pylint: disable=consider-using-dict-items
binary_sensors.append(
OpenEIBinarySensor(BINARY_SENSORS[binary_sensor], entry, coordinator)
)

async_add_devices(binary_sensors, False)

Expand All @@ -26,7 +31,6 @@ class OpenEIBinarySensor(CoordinatorEntity, BinarySensorEntity):

def __init__(
self,
hass: HomeAssistant,
sensor_description: BinarySensorEntityDescription,
entry: ConfigEntry,
coordinator: str,
Expand Down Expand Up @@ -62,4 +66,4 @@ def device_info(self) -> DeviceInfo:
identifiers={(DOMAIN, self._config.entry_id)},
manufacturer="OpenEI",
name="OpenEI",
)
)
Loading

0 comments on commit bb824a5

Please sign in to comment.