Skip to content

Commit

Permalink
Add channel accessibility check
Browse files Browse the repository at this point in the history
  • Loading branch information
alengwenus committed Feb 16, 2024
1 parent a5b128f commit b020b2f
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 60 deletions.
10 changes: 10 additions & 0 deletions custom_components/smaev/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@

from .const import (
DOMAIN,
SMAEV_CHANNELS,
SMAEV_COORDINATOR,
SMAEV_DEVICE_INFO,
SMAEV_MEASUREMENT,
SMAEV_OBJECT,
SMAEV_PARAMETER,
)
from .coordinator import SmaEvChargerCoordinator
from .services import async_setup_services, async_unload_services
Expand Down Expand Up @@ -79,12 +82,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
sw_version=smaev_device_info["sw_version"],
)

measurement_channels = await evcharger.get_measurement_channels()
parameter_channels = await evcharger.get_parameter_channels()

coordinator = SmaEvChargerCoordinator(hass, entry, evcharger)

hass.data[DOMAIN][entry.entry_id] = {
SMAEV_OBJECT: evcharger,
SMAEV_DEVICE_INFO: device_info,
SMAEV_COORDINATOR: coordinator,
SMAEV_CHANNELS: {
SMAEV_MEASUREMENT: measurement_channels,
SMAEV_PARAMETER: parameter_channels,
},
}

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
Expand Down
1 change: 1 addition & 0 deletions custom_components/smaev/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Constants for the SMA EV Charger integration."""
DOMAIN = "smaev"

SMAEV_CHANNELS = "channels"
SMAEV_COORDINATOR = "coordinator"
SMAEV_OBJECT = "pysmaev"
SMAEV_DEVICE_INFO = "device_info"
Expand Down
15 changes: 11 additions & 4 deletions custom_components/smaev/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from . import generate_smaev_entity_id
from .const import (
DOMAIN,
SMAEV_CHANNELS,
SMAEV_COORDINATOR,
SMAEV_DEVICE_INFO,
SMAEV_PARAMETER,
Expand Down Expand Up @@ -70,11 +71,17 @@ async def async_setup_entry(
entities = []

for entity_description in DATETIME_DESCRIPTIONS:
entities.append(
SmaEvChargerDateTime(
hass, coordinator, config_entry, device_info, entity_description
if entity_description.channel in data[SMAEV_CHANNELS][entity_description.type]:
entities.append(
SmaEvChargerDateTime(
hass, coordinator, config_entry, device_info, entity_description
)
)
else:
_LOGGER.warning(
"Channel '%s' is not accessible. Elevated rights might be required.",
entity_description.channel,
)
)

async_add_entities(entities)

Expand Down
27 changes: 15 additions & 12 deletions custom_components/smaev/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
from typing import TYPE_CHECKING

from pysmaev.exceptions import SmaEvChargerChannelError
from pysmaev.helpers import get_parameters_channel

from homeassistant.components.number import (
Expand Down Expand Up @@ -33,6 +32,7 @@
from . import generate_smaev_entity_id
from .const import (
DOMAIN,
SMAEV_CHANNELS,
SMAEV_COORDINATOR,
SMAEV_DEFAULT_MAX,
SMAEV_DEFAULT_MIN,
Expand Down Expand Up @@ -125,11 +125,17 @@ async def async_setup_entry(

entities = []
for entity_description in NUMBER_DESCRIPTIONS:
entities.append(
SmaEvChargerNumber(
hass, coordinator, config_entry, device_info, entity_description
if entity_description.channel in data[SMAEV_CHANNELS][entity_description.type]:
entities.append(
SmaEvChargerNumber(
hass, coordinator, config_entry, device_info, entity_description
)
)
else:
_LOGGER.warning(
"Channel '%s' is not accessible. Elevated rights might be required.",
entity_description.channel,
)
)

async_add_entities(entities)

Expand Down Expand Up @@ -165,13 +171,10 @@ def __init__(
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
try:
channel = get_parameters_channel(
self.coordinator.data[SMAEV_PARAMETER],
self.entity_description.channel,
)
except SmaEvChargerChannelError:
return
channel = get_parameters_channel(
self.coordinator.data[SMAEV_PARAMETER],
self.entity_description.channel,
)

min_value = channel.get(SMAEV_MIN_VALUE)
max_value = channel.get(SMAEV_MAX_VALUE)
Expand Down
27 changes: 15 additions & 12 deletions custom_components/smaev/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import TYPE_CHECKING

from pysmaev.const import SmaEvChargerParameters
from pysmaev.exceptions import SmaEvChargerChannelError
from pysmaev.helpers import get_parameters_channel

from homeassistant.components.select import (
Expand All @@ -27,6 +26,7 @@
from . import generate_smaev_entity_id
from .const import (
DOMAIN,
SMAEV_CHANNELS,
SMAEV_COORDINATOR,
SMAEV_DEVICE_INFO,
SMAEV_PARAMETER,
Expand Down Expand Up @@ -93,11 +93,17 @@ async def async_setup_entry(
entities = []

for entity_description in SELECT_DESCRIPTIONS:
entities.append(
SmaEvChargerSelect(
hass, coordinator, config_entry, device_info, entity_description
if entity_description.channel in data[SMAEV_CHANNELS][entity_description.type]:
entities.append(
SmaEvChargerSelect(
hass, coordinator, config_entry, device_info, entity_description
)
)
else:
_LOGGER.warning(
"Channel '%s' is not accessible. Elevated rights might be required.",
entity_description.channel,
)
)

async_add_entities(entities)

Expand Down Expand Up @@ -137,13 +143,10 @@ def __init__(
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
try:
channel = get_parameters_channel(
self.coordinator.data[SMAEV_PARAMETER],
self.entity_description.channel,
)
except SmaEvChargerChannelError:
return
channel = get_parameters_channel(
self.coordinator.data[SMAEV_PARAMETER],
self.entity_description.channel,
)

possible_values = channel[SMAEV_POSSIBLE_VALUES]
value = channel[SMAEV_VALUE]
Expand Down
43 changes: 23 additions & 20 deletions custom_components/smaev/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import TYPE_CHECKING

from pysmaev.const import SmaEvChargerMeasurements
from pysmaev.exceptions import SmaEvChargerChannelError
from pysmaev.helpers import get_measurements_channel, get_parameters_channel

from homeassistant.components.sensor import (
Expand Down Expand Up @@ -36,6 +35,7 @@
from . import generate_smaev_entity_id
from .const import (
DOMAIN,
SMAEV_CHANNELS,
SMAEV_COORDINATOR,
SMAEV_DEVICE_INFO,
SMAEV_MEASUREMENT,
Expand Down Expand Up @@ -216,11 +216,17 @@ async def async_setup_entry(
entities = []

for entity_description in SENSOR_DESCRIPTIONS:
entities.append(
SmaEvChargerSensor(
hass, coordinator, config_entry, device_info, entity_description
if entity_description.channel in data[SMAEV_CHANNELS][entity_description.type]:
entities.append(
SmaEvChargerSensor(
hass, coordinator, config_entry, device_info, entity_description
)
)
else:
_LOGGER.warning(
"Channel '%s' is not accessible. Elevated rights might be required.",
entity_description.channel,
)
)

async_add_entities(entities)

Expand Down Expand Up @@ -253,21 +259,18 @@ def __init__(
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
try:
if self.entity_description.type == SMAEV_MEASUREMENT:
channel = get_measurements_channel(
self.coordinator.data[SMAEV_MEASUREMENT],
self.entity_description.channel,
)
value = channel[0][SMAEV_VALUE]
else: # SMAEV_PARAMETER
channel = get_parameters_channel(
self.coordinator.data[SMAEV_PARAMETER],
self.entity_description.channel,
)
value = channel[SMAEV_VALUE]
except SmaEvChargerChannelError:
return
if self.entity_description.type == SMAEV_MEASUREMENT:
channel = get_measurements_channel(
self.coordinator.data[SMAEV_MEASUREMENT],
self.entity_description.channel,
)
value = channel[0][SMAEV_VALUE]
else: # SMAEV_PARAMETER
channel = get_parameters_channel(
self.coordinator.data[SMAEV_PARAMETER],
self.entity_description.channel,
)
value = channel[SMAEV_VALUE]

value = self.entity_description.value_mapping.get(value, value)

Expand Down
27 changes: 15 additions & 12 deletions custom_components/smaev/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import TYPE_CHECKING, Any

from pysmaev.const import SmaEvChargerParameters
from pysmaev.exceptions import SmaEvChargerChannelError
from pysmaev.helpers import get_parameters_channel

from homeassistant.components.switch import (
Expand All @@ -27,6 +26,7 @@
from . import generate_smaev_entity_id
from .const import (
DOMAIN,
SMAEV_CHANNELS,
SMAEV_COORDINATOR,
SMAEV_DEVICE_INFO,
SMAEV_PARAMETER,
Expand Down Expand Up @@ -89,11 +89,17 @@ async def async_setup_entry(
entities = []

for entity_description in SWITCH_DESCRIPTIONS:
entities.append(
SmaEvChargerSwitch(
hass, coordinator, config_entry, device_info, entity_description
if entity_description.channel in data[SMAEV_CHANNELS][entity_description.type]:
entities.append(
SmaEvChargerSwitch(
hass, coordinator, config_entry, device_info, entity_description
)
)
else:
_LOGGER.warning(
"Channel '%s' is not accessible. Elevated rights might be required.",
entity_description.channel,
)
)

async_add_entities(entities)

Expand Down Expand Up @@ -131,13 +137,10 @@ def __init__(
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
try:
channel = get_parameters_channel(
self.coordinator.data[SMAEV_PARAMETER],
self.entity_description.channel,
)
except SmaEvChargerChannelError:
return
channel = get_parameters_channel(
self.coordinator.data[SMAEV_PARAMETER],
self.entity_description.channel,
)

value = channel[SMAEV_VALUE]
self._attr_is_on = self.entity_description.value_mapping.get(value, value)
Expand Down

0 comments on commit b020b2f

Please sign in to comment.