Skip to content

Commit

Permalink
Add option to select max charging amperage & Deprecate services
Browse files Browse the repository at this point in the history
  • Loading branch information
stickpin committed Jun 8, 2024
1 parent ce0e481 commit 859c08d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 334 deletions.
25 changes: 3 additions & 22 deletions custom_components/volkswagencarnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
Instrument,
Number,
Position,
Select,
Sensor,
Switch,
TrunkLock,
Expand All @@ -55,34 +56,18 @@
DEFAULT_DEBUG,
DEFAULT_UPDATE_INTERVAL,
DOMAIN,
SERVICE_SET_CHARGER_MAX_CURRENT,
SIGNAL_STATE_UPDATED,
UNDO_UPDATE_LISTENER,
UPDATE_CALLBACK,
)
from .services import SERVICE_SET_CHARGER_MAX_CURRENT_SCHEMA, ChargerService
from .util import get_convert_conf

_LOGGER = logging.getLogger(__name__)


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


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Perform Volkswagen Connect component setup."""

def register_services():
cs = ChargerService(hass)
hass.services.async_register(
domain=DOMAIN,
service=SERVICE_SET_CHARGER_MAX_CURRENT,
service_func=cs.set_charger_max_current,
schema=SERVICE_SET_CHARGER_MAX_CURRENT_SCHEMA,
)

scan_interval_conf = entry.options.get(
CONF_SCAN_INTERVAL, entry.data.get(CONF_SCAN_INTERVAL, DEFAULT_UPDATE_INTERVAL)
)
Expand Down Expand Up @@ -139,8 +124,6 @@ def is_new(attr):
hass.config_entries.async_forward_entry_setup(entry, component)
)

register_services()

return True


Expand Down Expand Up @@ -191,8 +174,6 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool:

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
_LOGGER.debug("Removing services")
unload_services(hass)
_LOGGER.debug("Removing update listener")
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()

Expand Down Expand Up @@ -264,8 +245,7 @@ def vehicle_name(self, vehicle: Vehicle) -> str:

if vehicle.vin:
return vehicle.vin
else:
return ""
return ""


class VolkswagenEntity(CoordinatorEntity, RestoreEntity):
Expand Down Expand Up @@ -365,6 +345,7 @@ def instrument(
BinarySensor
| DoorLock
| Position
| Select
| Sensor
| Switch
| TrunkLock
Expand Down
3 changes: 1 addition & 2 deletions custom_components/volkswagencarnet/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
"binary_sensor": "binary_sensor",
"lock": "lock",
"device_tracker": "device_tracker",
"select": "select",
"switch": "switch",
"number": "number",
}

SERVICE_SET_CHARGER_MAX_CURRENT = "set_charger_max_current"
75 changes: 75 additions & 0 deletions custom_components/volkswagencarnet/select.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Number support for Volkswagen Connect integration."""

import logging

from homeassistant.components.select import SelectEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory

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

_LOGGER = logging.getLogger(__name__)


async def async_setup_platform(
hass: HomeAssistant, config, async_add_entities, discovery_info=None
):
"""Set up the Volkswagen select platform."""
if discovery_info is None:
return
async_add_entities([VolkswagenSelect(hass.data[DATA_KEY], *discovery_info)])


async def async_setup_entry(hass: HomeAssistant, entry, async_add_devices):
"""Set up the Volkswagen select."""
data = hass.data[DOMAIN][entry.entry_id][DATA]
coordinator = data.coordinator
if coordinator.data is not None:
async_add_devices(
VolkswagenSelect(
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 == "select"
)
)

return True


class VolkswagenSelect(VolkswagenEntity, SelectEntity):
"""Representation of a Volkswagen select."""

@property
def options(self) -> list:
"""Return the options list."""
if self.instrument.options:
return self.instrument.options
return None

@property
def current_option(self) -> str:
"""Return the current option."""
if self.instrument.current_option:
return self.instrument.current_option
return None

@property
def entity_category(self) -> EntityCategory | str | None:
"""Return entity category."""
if self.instrument.entity_type == "diag":
return EntityCategory.DIAGNOSTIC
if self.instrument.entity_type == "config":
return EntityCategory.CONFIG

async def async_select_option(self, option: str) -> None:
"""Update the current value."""
await self.instrument.set_value(option)
self.notify_updated()
48 changes: 0 additions & 48 deletions custom_components/volkswagencarnet/services.py

This file was deleted.

Loading

0 comments on commit 859c08d

Please sign in to comment.