Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Performance #5

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ frontend:
# automation: !include automations.yaml
# script: !include scripts.yaml
# scene: !include scenes.yaml

logger:
default: "info"
9 changes: 5 additions & 4 deletions custom_components/adaptive_cover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hass.config_entries.async_forward_entry_setups(entry, (Platform.SENSOR,))

await async_initialize_integration(hass=hass, config_entry=entry)

entry.async_on_unload(entry.add_update_listener(config_entry_update_listener))

return True


async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener, called when the config entry options are changed."""
await hass.config_entries.async_reload(entry.entry_id)
await async_initialize_integration(hass=hass, config_entry=entry)



async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(
entry, (Platform.SENSOR,)
):
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok
return unload_ok
2 changes: 1 addition & 1 deletion custom_components/adaptive_cover/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"documentation": "https://github.com/basbruss/test",
"iot_class": "calculated",
"issue_tracker": "https://github.com/basbruss/adaptive-cover/issues",
"requirements": ["astral"],
"requirements": ["astral","pandas"],
"version": "0.0.1"
}
51 changes: 27 additions & 24 deletions custom_components/adaptive_cover/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
from homeassistant.components.sensor import SensorEntity, SensorStateClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, EVENT_HOMEASSISTANT_STARTED
from homeassistant.core import HomeAssistant, CoreState
from homeassistant.core import HomeAssistant, CoreState, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import UndefinedType
from homeassistant.helpers.typing import UndefinedType, EventType
from homeassistant.helpers.event import (
EventStateChangedData,
async_track_state_change_event,
)

from .calculation import AdaptiveCoverCalculator

Expand All @@ -33,9 +37,6 @@
SensorType,
)

SCAN_INTERVAL = timedelta(seconds=10)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
Expand All @@ -55,14 +56,14 @@ async def async_setup_entry(

async_add_entities([sensor], False)


class AdaptiveCoverSensorEntity(SensorEntity):
"""adaptive_cover Sensor."""

_attr_state_class = SensorStateClass.MEASUREMENT
_attr_native_unit_of_measurement = PERCENTAGE
_attr_icon = "mdi:sun-compass"
_attr_has_entity_name = True
_attr_should_poll = False

def __init__(
self,
Expand All @@ -85,17 +86,29 @@ def __init__(
self._attr_name = f"Adaptive Cover {name} {type[self.config_entry.data[CONF_SENSOR_TYPE]]}"
self._name = f"Adaptive Cover {name}"

@callback
def async_on_state_change(self, event: EventType[EventStateChangedData]) -> None:
"""Update supported features and state when a new state is received."""
self.async_set_context(event.context)

self.async_update_state()

async def async_added_to_hass(self) -> None:
"""Handle when entity is added."""
if self.hass.state != CoreState.running:
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STARTED, self.first_update
)
else:
await self.first_update()
"Handle added to Hass"
async_track_state_change_event(
self.hass, ["sun.sun"], self.async_on_state_change
)
self.async_update_state()

@callback
def async_update_state(self) -> None:
"Determine state after push"
self._cover_data.update()

self.async_write_ha_state()

@property
def native_value(self) -> int | None:
def native_value(self) -> str | None:
"""Handle when entity is added"""
return self._cover_data.state

Expand Down Expand Up @@ -125,16 +138,6 @@ def extra_state_attributes(self) -> Mapping[str, Any] | None:
dict_attributes["distance"] = self.config_entry.options[CONF_DISTANCE]
return dict_attributes

async def first_update(self, _=None) -> None:
"""Run first update and write state."""
await self.hass.async_add_executor_job(self.update)
self.async_write_ha_state()

def update(self) -> None:
"""Fetch new state data for the sensor."""
self._cover_data.update()


class AdaptiveCoverData:
"""AdaptiveCover data object"""

Expand Down