Skip to content

Commit

Permalink
Run formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Oct 22, 2022
1 parent 20eb334 commit 32244ea
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
38 changes: 29 additions & 9 deletions custom_components/powercalc/power_profile/power_profile.py
Expand Up @@ -12,9 +12,9 @@
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.typing import ConfigType

from ..common import SourceEntity
from ..const import CalculationStrategy
from ..errors import ModelNotSupported, PowercalcSetupError, UnsupportedMode
from ..common import SourceEntity

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -204,7 +204,12 @@ def is_entity_domain_supported(self, domain: str) -> bool:


class SubProfileSelector:
def __init__(self, hass: HomeAssistant, power_profile: PowerProfile, source_entity: SourceEntity | None = None):
def __init__(
self,
hass: HomeAssistant,
power_profile: PowerProfile,
source_entity: SourceEntity | None = None,
):
self._hass = hass
self._power_profile = power_profile
self._source_entity = source_entity
Expand All @@ -220,9 +225,7 @@ def _build_matchers(self) -> list[SubProfileMatcher]:
matchers.append(self._create_matcher(matcher_config))
return matchers

def select_sub_profile(
self, entity_state: State
) -> str:
def select_sub_profile(self, entity_state: State) -> str:
"""
Dynamically tries to select a sub profile depending on the entity state.
This method always need to return a sub profile, when nothing is matched it will return a default
Expand All @@ -236,15 +239,24 @@ def select_sub_profile(

def get_tracking_entities(self) -> list[str]:
"""Get additional list of entities to track for state changes"""
return [entity_id for matcher in self._matchers for entity_id in matcher.get_tracking_entities()]
return [
entity_id
for matcher in self._matchers
for entity_id in matcher.get_tracking_entities()
]

def _create_matcher(self, matcher_config: dict) -> SubProfileMatcher:
"""Create a matcher from json config. Can be extended for more matchers in the future"""
matcher_type: str = matcher_config["type"]
if matcher_type == "attribute":
return AttributeMatcher(matcher_config["attribute"], matcher_config["map"])
if matcher_type == "entity_state":
return EntityStateMatcher(self._hass, self._source_entity, matcher_config["entity_id"], matcher_config["map"])
return EntityStateMatcher(
self._hass,
self._source_entity,
matcher_config["entity_id"],
matcher_config["map"],
)
raise PowercalcSetupError(f"Unknown sub profile matcher type: {matcher_type}")


Expand All @@ -264,10 +276,18 @@ def get_tracking_entities(self) -> list[str]:


class EntityStateMatcher(SubProfileMatcher):
def __init__(self, hass: HomeAssistant, source_entity: SourceEntity, entity_id: str, mapping: dict[str, str]):
def __init__(
self,
hass: HomeAssistant,
source_entity: SourceEntity,
entity_id: str,
mapping: dict[str, str],
):
self._hass = hass
if source_entity:
entity_id = entity_id.replace("{{source_object_id}}", source_entity.object_id)
entity_id = entity_id.replace(
"{{source_object_id}}", source_entity.object_id
)
self._entity_id = entity_id
self._mapping = mapping
pass
Expand Down
6 changes: 4 additions & 2 deletions custom_components/powercalc/sensors/power.py
Expand Up @@ -319,7 +319,7 @@ def __init__(
ATTR_SOURCE_DOMAIN: source_entity.domain,
}
self._power_profile = power_profile
self._sub_profile_selector: SubProfileSelector | None = None
self._sub_profile_selector: SubProfileSelector | None = None

async def async_added_to_hass(self):
"""Register callbacks."""
Expand Down Expand Up @@ -351,7 +351,9 @@ async def initial_update(event):
track_entities = [self._source_entity.entity_id]

if self._power_profile:
self._sub_profile_selector = SubProfileSelector(self.hass, self._power_profile, self._source_entity)
self._sub_profile_selector = SubProfileSelector(
self.hass, self._power_profile, self._source_entity
)
track_entities.extend(self._sub_profile_selector.get_tracking_entities())

self._track_entities = track_entities
Expand Down
8 changes: 2 additions & 6 deletions tests/power_profile/device_types/test_infrared_light.py
Expand Up @@ -55,16 +55,12 @@ async def test_infrared_light(hass: HomeAssistant):
ATTR_COLOR_TEMP: 601,
},
)
hass.states.async_set(
infrared_brightness_select_id, "50%"
)
hass.states.async_set(infrared_brightness_select_id, "50%")
await hass.async_block_till_done()

assert hass.states.get(power_sensor_id).state == "4.37"

hass.states.async_set(
infrared_brightness_select_id, "25%"
)
hass.states.async_set(infrared_brightness_select_id, "25%")
await hass.async_block_till_done()

assert hass.states.get(power_sensor_id).state == "2.59"
1 change: 0 additions & 1 deletion tests/power_profile/test_power_profile.py
Expand Up @@ -126,4 +126,3 @@ async def test_selecting_sub_profile_is_ignored(hass: HomeAssistant) -> None:

power_profile.select_sub_profile("foo")
assert not power_profile.sub_profile

1 change: 0 additions & 1 deletion tests/sensors/test_power.py
Expand Up @@ -384,4 +384,3 @@ async def test_sleep_power(hass: HomeAssistant):
await hass.async_block_till_done()

assert hass.states.get(power_entity_id).state == "100.00"

0 comments on commit 32244ea

Please sign in to comment.