Skip to content

Commit

Permalink
Merge pull request #579 from robinostlund/v4.5.5
Browse files Browse the repository at this point in the history
v4.5.5
  • Loading branch information
robinostlund authored Feb 25, 2024
2 parents c33e722 + e25cb57 commit 48d295a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 272 deletions.
34 changes: 4 additions & 30 deletions custom_components/volkswagencarnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Switch,
DoorLock,
Position,
Number,
TrunkLock,
)
from volkswagencarnet.vw_vehicle import Vehicle
Expand All @@ -55,20 +56,13 @@
DEFAULT_DEBUG,
CONF_CONVERT,
CONF_IMPERIAL_UNITS,
SERVICE_SET_TIMER_BASIC_SETTINGS,
SERVICE_UPDATE_SCHEDULE,
SERVICE_UPDATE_PROFILE,
SERVICE_SET_CHARGER_MAX_CURRENT,
CONF_AVAILABLE_RESOURCES,
CONF_NO_CONVERSION,
)
from .services import (
SchedulerService,
ChargerService,
SERVICE_SET_TIMER_BASIC_SETTINGS_SCHEMA,
SERVICE_SET_CHARGER_MAX_CURRENT_SCHEMA,
SERVICE_UPDATE_SCHEDULE_SCHEMA,
SERVICE_UPDATE_PROFILE_SCHEMA,
)
from .util import get_convert_conf

Expand All @@ -77,9 +71,6 @@

def unload_services(hass: HomeAssistant):
"""Unload the services from HA."""
hass.services.async_remove(DOMAIN, SERVICE_SET_TIMER_BASIC_SETTINGS)
hass.services.async_remove(DOMAIN, SERVICE_UPDATE_SCHEDULE)
hass.services.async_remove(DOMAIN, SERVICE_UPDATE_PROFILE)
hass.services.async_remove(DOMAIN, SERVICE_SET_CHARGER_MAX_CURRENT)


Expand All @@ -88,25 +79,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

def register_services():
cs = ChargerService(hass)
ss = SchedulerService(hass)
hass.services.async_register(
domain=DOMAIN,
service=SERVICE_SET_TIMER_BASIC_SETTINGS,
service_func=ss.set_timer_basic_settings,
schema=SERVICE_SET_TIMER_BASIC_SETTINGS_SCHEMA,
)
hass.services.async_register(
domain=DOMAIN,
service=SERVICE_UPDATE_SCHEDULE,
service_func=ss.update_schedule,
schema=SERVICE_UPDATE_SCHEDULE_SCHEMA,
)
hass.services.async_register(
domain=DOMAIN,
service=SERVICE_UPDATE_PROFILE,
service_func=ss.update_profile,
schema=SERVICE_UPDATE_PROFILE_SCHEMA,
)
hass.services.async_register(
domain=DOMAIN,
service=SERVICE_SET_CHARGER_MAX_CURRENT,
Expand Down Expand Up @@ -345,6 +317,8 @@ def async_write_ha_state(self) -> None:
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 Expand Up @@ -378,7 +352,7 @@ async def async_added_to_hass(self) -> None:
@property
def instrument(
self,
) -> Union[BinarySensor, Climate, DoorLock, Position, Sensor, Switch, TrunkLock, Instrument]:
) -> Union[BinarySensor, Climate, DoorLock, Position, Sensor, Switch, TrunkLock, Number, Instrument]:
"""Return corresponding instrument."""
return self.data.instrument(self.vin, self.component, self.attribute)

Expand Down
1 change: 1 addition & 0 deletions custom_components/volkswagencarnet/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"device_tracker": "device_tracker",
"switch": "switch",
"climate": "climate",
"number": "number",
}

SERVICE_SET_TIMER_BASIC_SETTINGS = "set_timer_basic_settings"
Expand Down
93 changes: 93 additions & 0 deletions custom_components/volkswagencarnet/number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""Number support for Volkswagen We Connect integration."""

import logging
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 .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):
"""Set up the Volkswagen number platform."""
if discovery_info is None:
return
async_add_entities([VolkswagenNumber(hass.data[DATA_KEY], *discovery_info)])


async def async_setup_entry(hass, entry, async_add_devices):
"""Set up the Volkswagen number."""
data = hass.data[DOMAIN][entry.entry_id][DATA]
coordinator = data.coordinator
if coordinator.data is not None:
async_add_devices(
VolkswagenNumber(
data=data,
vin=coordinator.vin,
component=instrument.component,
attribute=instrument.attr,
callback=hass.data[DOMAIN][entry.entry_id][UPDATE_CALLBACK],
)
for instrument in (instrument for instrument in data.instruments if instrument.component == "number")
)

return True


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."""
if self.instrument.min_value:
return self.instrument.min_value
return None

@property
def native_max_value(self) -> float:
"""Return the maximum value."""
if self.instrument.max_value:
return self.instrument.max_value
return None

@property
def native_step(self) -> float:
if self.instrument.native_step:
return self.instrument.native_step
return None

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

@property
def entity_category(self) -> Union[EntityCategory, str, None]:
"""Return entity category."""
if self.instrument.entity_type == "diag":
return EntityCategory.DIAGNOSTIC
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()
Loading

0 comments on commit 48d295a

Please sign in to comment.