Skip to content

Commit

Permalink
Fixes for HA 2023.4 (#2108)
Browse files Browse the repository at this point in the history
* Fixes for HA 2023.4

* ruff fixes

* mypy fixes
  • Loading branch information
bramstroker committed Mar 8, 2024
1 parent 4b6c9fa commit a424b13
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion custom_components/powercalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
if unload_ok:
used_unique_ids: list[str] = hass.data[DOMAIN][DATA_USED_UNIQUE_IDS]
try:
used_unique_ids.remove(config_entry.unique_id)
if config_entry.unique_id:
used_unique_ids.remove(config_entry.unique_id)
except ValueError:
return True

Expand Down
2 changes: 1 addition & 1 deletion custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ def _create_group_selector(
options = [
selector.SelectOptionDict(
value=config_entry.entry_id,
label=config_entry.data.get(CONF_NAME),
label=str(config_entry.data.get(CONF_NAME)),
)
for config_entry in hass.config_entries.async_entries(DOMAIN)
if config_entry.data.get(CONF_SENSOR_TYPE) == SensorType.GROUP
Expand Down
2 changes: 1 addition & 1 deletion custom_components/powercalc/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def _load_manually_configured_entities(self) -> list[str]:
# Add entities from existing config entries
entities.extend(
[
entry.data.get(CONF_ENTITY_ID)
str(entry.data.get(CONF_ENTITY_ID))
for entry in self.hass.config_entries.async_entries(DOMAIN)
if entry.source == SOURCE_USER
],
Expand Down
4 changes: 2 additions & 2 deletions custom_components/powercalc/group_include/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def find_powercalc_entities_by_source_entity(
if entry.data.get(CONF_ENTITY_ID) != source_entity_id:
continue
if entry.data.get(ENTRY_DATA_POWER_ENTITY):
entities.append(RealPowerSensor(entry.data.get(ENTRY_DATA_POWER_ENTITY)))
entities.append(RealPowerSensor(str(entry.data.get(ENTRY_DATA_POWER_ENTITY))))
if entry.data.get(ENTRY_DATA_ENERGY_ENTITY):
entities.append(RealEnergySensor(entry.data.get(ENTRY_DATA_ENERGY_ENTITY)))
entities.append(RealEnergySensor(str(entry.data.get(ENTRY_DATA_ENERGY_ENTITY))))
return entities


Expand Down
10 changes: 4 additions & 6 deletions custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from homeassistant.helpers.singleton import singleton
from homeassistant.helpers.storage import Store
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import Throttle
from homeassistant.util.unit_conversion import (
EnergyConverter,
PowerConverter,
Expand Down Expand Up @@ -171,7 +170,7 @@ async def create_group_sensors_from_config_entry(
"""Create group sensors based on a config_entry."""
group_sensors: list[SensorEntity] = []

group_name = entry.data.get(CONF_NAME)
group_name = str(entry.data.get(CONF_NAME))

if CONF_UNIQUE_ID not in sensor_config:
sensor_config[CONF_UNIQUE_ID] = entry.entry_id
Expand Down Expand Up @@ -285,7 +284,7 @@ async def add_to_associated_group(
if CONF_GROUP not in config_entry.data:
return None

group_entry_id = config_entry.data.get(CONF_GROUP)
group_entry_id = str(config_entry.data.get(CONF_GROUP))
group_entry = hass.config_entries.async_get_entry(group_entry_id)

if not group_entry:
Expand Down Expand Up @@ -334,7 +333,7 @@ async def resolve_entity_ids_recursively(
if key not in member_entry.data: # pragma: no cover
continue

resolved_ids.update([member_entry.data.get(key)])
resolved_ids.update([str(member_entry.data.get(key))])

# Include the additional power/energy sensors the user specified
conf_key = (
Expand Down Expand Up @@ -517,8 +516,6 @@ async def async_added_to_hass(self) -> None:
err,
)

state_listener = Throttle(timedelta(seconds=30))(state_listener)

self._prev_state_store = await PreviousStateStore.async_get_instance(self.hass)

if isinstance(self, GroupedPowerSensor):
Expand Down Expand Up @@ -560,6 +557,7 @@ def _async_hide_members(self, hide: bool) -> None:
@callback
def on_state_change(self, _: Any) -> None: # noqa
"""Triggered when one of the group entities changes state."""

all_states = [self.hass.states.get(entity_id) for entity_id in self._entities]
states: list[State] = list(filter(None, all_states))
available_states = [
Expand Down
2 changes: 1 addition & 1 deletion tests/group_include/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ async def test_include_logs_warning(hass: HomeAssistant, caplog: pytest.LogCaptu
def _create_powercalc_config_entry(
hass: HomeAssistant,
source_entity_id: str,
unique_id: str | None = None
unique_id: str | None = None,
) -> MockConfigEntry:
__, object_id = split_entity_id(source_entity_id)

Expand Down
4 changes: 4 additions & 0 deletions tests/sensors/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ async def test_calibrate_service(hass: HomeAssistant) -> None:
},
)

hass.states.async_set("sensor.test1_energy", "20")
await hass.async_block_till_done()

await hass.services.async_call(
DOMAIN,
SERVICE_CALIBRATE_ENERGY,
Expand Down Expand Up @@ -372,6 +375,7 @@ async def test_restore_state(hass: HomeAssistant) -> None:
CONF_ENTITIES: [
get_simple_fixed_config("input_boolean.test1"),
],
CONF_IGNORE_UNAVAILABLE_STATE: True,
},
)

Expand Down
3 changes: 2 additions & 1 deletion tests/testing_config/custom_components/test/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Call init before using it in your tests to ensure clean test data.
"""
import uuid
from typing import ClassVar

from homeassistant.components.light import ColorMode, LightEntity, LightEntityFeature
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -56,7 +57,7 @@ class MockLight(MockToggleEntity, LightEntity):
color_mode = ColorMode.BRIGHTNESS
max_mireds = 500
min_mireds = 153
supported_color_modes = [ColorMode.BRIGHTNESS]
supported_color_modes: ClassVar = [ColorMode.BRIGHTNESS]
supported_features = LightEntityFeature(0)

brightness = None
Expand Down

0 comments on commit a424b13

Please sign in to comment.