Skip to content

Commit

Permalink
Drop Requests remaining sensor & code clean up
Browse files Browse the repository at this point in the history
WIP - ID models features
  • Loading branch information
stickpin committed Feb 27, 2024
1 parent ac9bdc7 commit 6f91380
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 38 deletions.
18 changes: 0 additions & 18 deletions custom_components/volkswagencarnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
CONF_USERNAME,
EVENT_HOMEASSISTANT_STOP,
STATE_UNKNOWN,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant, Event, callback, State
from homeassistant.helpers.aiohttp_client import async_get_clientsession
Expand Down Expand Up @@ -292,20 +291,6 @@ def async_write_ha_state(self) -> None:
# Get the previous state from the state machine if found
prev: Optional[State] = self.hass.states.get(self.entity_id)

# This is not the best place to handle this, but... :shrug:..
if self.attribute == "requests_remaining" and self.state in [-1, STATE_UNAVAILABLE, STATE_UNKNOWN]:
restored = prev or self.restored_state
if restored is not None:
try:
value = int(restored.state)
_LOGGER.debug(f"Restoring requests remaining to '{restored.state}'")
self.vehicle.requests_remaining = value
except ValueError:
pass
else:
_LOGGER.debug(f"Not changing requests remaining to '{self.state}'")
return

# need to persist state if:
# - there is no previous state
# - there is no information about when the last backend update was done
Expand All @@ -315,10 +300,7 @@ def async_write_ha_state(self) -> None:
prev is None
or str(prev.attributes.get("last_updated", None)) != str(backend_refresh_time)
or str(self.state or STATE_UNKNOWN) != str(prev.state)
or self.component == "climate"
):
if self.component == "climate":
self._update_state()
super().async_write_ha_state()
else:
_LOGGER.debug(f"{self.name}: state not changed ('{prev.state}' == '{self.state}'), skipping update.")
Expand Down
3 changes: 0 additions & 3 deletions custom_components/volkswagencarnet/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,4 @@
"number": "number",
}

SERVICE_SET_TIMER_BASIC_SETTINGS = "set_timer_basic_settings"
SERVICE_UPDATE_SCHEDULE = "update_schedule"
SERVICE_UPDATE_PROFILE = "update_profile"
SERVICE_SET_CHARGER_MAX_CURRENT = "set_charger_max_current"
29 changes: 12 additions & 17 deletions custom_components/volkswagencarnet/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
from typing import Union

from homeassistant.components.number import NumberEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory

from . import VolkswagenEntity, VolkswagenData
from . import VolkswagenEntity
from .const import DATA_KEY, DATA, DOMAIN, UPDATE_CALLBACK

_LOGGER = logging.getLogger(__name__)


async def async_setup_platform(hass: HomeAssistant, config: ConfigEntry, async_add_entities, discovery_info=None):
async def async_setup_platform(hass: HomeAssistant, config, async_add_entities, discovery_info=None):
"""Set up the Volkswagen number platform."""
if discovery_info is None:
return
Expand Down Expand Up @@ -43,10 +42,6 @@ async def async_setup_entry(hass, entry, async_add_devices):
class VolkswagenNumber(VolkswagenEntity, NumberEntity):
"""Representation of a Volkswagen number."""

def __init__(self, data: VolkswagenData, vin: str, component: str, attribute: str, callback=None):
"""Initialize switch."""
super().__init__(data, vin, component, attribute, callback)

@property
def native_min_value(self) -> float:
"""Return the minimum value."""
Expand All @@ -63,16 +58,22 @@ def native_max_value(self) -> float:

@property
def native_step(self) -> float:
if self.instrument.native_step:
return self.instrument.native_step
return None
"""Return the increment/decrement step."""
return self.instrument.native_step

@property
def native_unit_of_measurement(self) -> str:
if self.instrument.unit:
return self.instrument.unit
return ""

@property
def native_value(self) -> float:
"""Return the entity value to represent the entity state."""
if self.instrument.state:
return self.instrument.state
return None

@property
def entity_category(self) -> Union[EntityCategory, str, None]:
"""Return entity category."""
Expand All @@ -81,13 +82,7 @@ def entity_category(self) -> Union[EntityCategory, str, None]:
if self.instrument.entity_type == "config":
return EntityCategory.CONFIG

@property
def native_value(self) -> float:
"""Return the entity value to represent the entity state."""
return self.instrument.state

async def async_set_native_value(self, value: int) -> None:
"""Update the current value."""
_LOGGER.debug("Update current value to %s." % value)
await self.instrument.set_value(value)
# self.notify_updated()
self.notify_updated()

0 comments on commit 6f91380

Please sign in to comment.