From e27f87710c99ab9d04a82e35c448fbc3f407c3a0 Mon Sep 17 00:00:00 2001 From: Geert Meersman Date: Wed, 12 Apr 2023 18:34:05 +0200 Subject: [PATCH] Device PIN added as a sensor --- custom_components/nexxtmove/client.py | 56 +++++++++++++++++++++ custom_components/nexxtmove/config_flow.py | 8 +-- custom_components/nexxtmove/const.py | 2 +- custom_components/nexxtmove/entity.py | 2 +- custom_components/nexxtmove/models.py | 1 + custom_components/nexxtmove/sensor.py | 57 ++++++++++++---------- 6 files changed, 94 insertions(+), 32 deletions(-) diff --git a/custom_components/nexxtmove/client.py b/custom_components/nexxtmove/client.py index f79868d..41ed3ed 100644 --- a/custom_components/nexxtmove/client.py +++ b/custom_components/nexxtmove/client.py @@ -125,6 +125,7 @@ def fetch_data(self): name=company.get("name"), key=key, type="company", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, @@ -141,6 +142,7 @@ def fetch_data(self): name=location.get("name"), key=key, type="work_location", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, @@ -156,6 +158,7 @@ def fetch_data(self): name="Profile", key=key, type="profile", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, @@ -169,6 +172,7 @@ def fetch_data(self): name="Recent charges", key=key, type="charges", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, @@ -182,6 +186,7 @@ def fetch_data(self): name="Consumption", key=key, type="consumption", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, @@ -197,6 +202,7 @@ def fetch_data(self): name=location.get("name"), key=key, type="residential_location", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, @@ -216,12 +222,46 @@ def fetch_data(self): name=charging_device.get("name"), key=key, type="charging_device", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, state=charging_device.get("buildingName"), extra_attributes=charging_device, ) + + """ + #Switch will be used when it becomes useful + key = format_entity_name( + f"{self.username} charging device {charging_device.get('id')} switch" + ) + data[key] = NexxtmoveItem( + name=charging_device.get("name"), + key=key, + type="charging_device", + sensor_type="switch", + device_key=device_key, + device_name=device_name, + device_model=device_model, + state=False, + ) + """ + + pin = self.device_pin(charging_device.get("id")) + key = format_entity_name( + f"{self.username} charging device {charging_device.get('id')} pin" + ) + data[key] = NexxtmoveItem( + name=f"{charging_device.get('name')} PIN", + key=key, + type="charging_device_pin", + sensor_type="sensor", + device_key=device_key, + device_name=device_name, + device_model=device_model, + state=pin.get("pin"), + ) + events = self.device_events(charging_device.get("id")) if events.get("events") and len(events.get("events")): key = format_entity_name( @@ -231,6 +271,7 @@ def fetch_data(self): name=f"{charging_device.get('name')} events", key=key, type="charging_events", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, @@ -247,6 +288,7 @@ def fetch_data(self): name=charging_point.get("name"), key=key, type="charging_point", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, @@ -262,6 +304,7 @@ def fetch_data(self): name=f"{charging_point.get('name')} events", key=key, type="charging_events", + sensor_type="sensor", device_key=device_key, device_name=device_name, device_model=device_model, @@ -311,6 +354,19 @@ def device_events(self, device_id): return False return response.json() + def device_pin(self, device_id): + """Fetch Device pin.""" + log_debug("[NexxtmoveClient|device_pin] Fetching device pin from Nexxtmove") + response = self.request( + f"{self.environment.api_endpoint}/device/{device_id}/pin", + "[NexxtmoveClient|device_pin]", + None, + 200, + ) + if response is False: + return False + return response.json() + def charging_point(self, charging_point_id): """Fetch Charging point info.""" log_debug( diff --git a/custom_components/nexxtmove/config_flow.py b/custom_components/nexxtmove/config_flow.py index e8f0567..735156c 100644 --- a/custom_components/nexxtmove/config_flow.py +++ b/custom_components/nexxtmove/config_flow.py @@ -92,7 +92,9 @@ async def async_step_connection_init( ), } return self.async_show_form( - step_id="connection_init", data_schema=vol.Schema(fields),errors=errors, + step_id="connection_init", + data_schema=vol.Schema(fields), + errors=errors, ) async def test_connection(self, user_input: dict | None = None) -> dict: @@ -129,9 +131,7 @@ async def async_step_password(self, user_input: dict | None = None) -> FlowResul self.new_entry_data |= NexxtmoveConfigEntryData( password=user_input[CONF_PASSWORD], ) - log_debug( - f"Password changed for {user_input[CONF_USERNAME]}" - ) + log_debug(f"Password changed for {user_input[CONF_USERNAME]}") return self.finish_flow() fields = { diff --git a/custom_components/nexxtmove/const.py b/custom_components/nexxtmove/const.py index cb418f8..39fc12f 100644 --- a/custom_components/nexxtmove/const.py +++ b/custom_components/nexxtmove/const.py @@ -13,7 +13,7 @@ _LOGGER = logging.getLogger(__name__) -PLATFORMS: Final = [Platform.SENSOR] +PLATFORMS: Final = [Platform.SENSOR, Platform.SWITCH] ATTRIBUTION: Final = "Data provided by Nexxtmove" diff --git a/custom_components/nexxtmove/entity.py b/custom_components/nexxtmove/entity.py index cf82036..c7d5220 100644 --- a/custom_components/nexxtmove/entity.py +++ b/custom_components/nexxtmove/entity.py @@ -57,7 +57,7 @@ def __init__( @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" - if len(self.coordinator.data): + if self.coordinator.data is not None and len(self.coordinator.data): for item in self.coordinator.data: item = self.coordinator.data[item] if self._key == item.key: diff --git a/custom_components/nexxtmove/models.py b/custom_components/nexxtmove/models.py index 88bd123..e145963 100644 --- a/custom_components/nexxtmove/models.py +++ b/custom_components/nexxtmove/models.py @@ -29,6 +29,7 @@ class NexxtmoveItem: name: str = "" key: str = "" type: str = "" + sensor_type: str = "" state: str = "" device_key: str = "" device_name: str = "" diff --git a/custom_components/nexxtmove/sensor.py b/custom_components/nexxtmove/sensor.py index b1614ea..f2b8a9f 100644 --- a/custom_components/nexxtmove/sensor.py +++ b/custom_components/nexxtmove/sensor.py @@ -31,6 +31,7 @@ class NexxtmoveSensorDescription(SensorEntityDescription): SENSOR_DESCRIPTIONS: list[SensorEntityDescription] = [ NexxtmoveSensorDescription(key="company", icon="mdi:account-group"), + NexxtmoveSensorDescription(key="charging_device_pin", icon="mdi:lock-question"), NexxtmoveSensorDescription(key="profile", icon="mdi:face-man"), NexxtmoveSensorDescription( key="consumption", @@ -68,34 +69,38 @@ async def async_setup_entry( if coordinator.data is not None: for item in coordinator.data: item = coordinator.data[item] - if description := SUPPORTED_KEYS.get(item.type): - if item.native_unit_of_measurement is not None: - native_unit_of_measurement = item.native_unit_of_measurement + if item.sensor_type == "sensor": + if description := SUPPORTED_KEYS.get(item.type): + if item.native_unit_of_measurement is not None: + native_unit_of_measurement = item.native_unit_of_measurement + else: + native_unit_of_measurement = ( + description.native_unit_of_measurement + ) + sensor_description = NexxtmoveSensorDescription( + key=str(item.key), + name=item.name, + value_fn=description.value_fn, + native_unit_of_measurement=native_unit_of_measurement, + icon=description.icon, + ) + + log_debug(f"[sensor|async_setup_entry|adding] {item.name}") + entities.append( + NexxtmoveSensor( + coordinator=coordinator, + description=sensor_description, + item=item, + ) + ) else: - native_unit_of_measurement = description.native_unit_of_measurement - sensor_description = NexxtmoveSensorDescription( - key=str(item.key), - name=item.name, - value_fn=description.value_fn, - native_unit_of_measurement=native_unit_of_measurement, - icon=description.icon, - ) - - log_debug(f"[sensor|async_setup_entry|adding] {item.name}") - entities.append( - NexxtmoveSensor( - coordinator=coordinator, - description=sensor_description, - item=item, + log_debug( + f"[sensor|async_setup_entry|no support type found] {item.name}, type: {item.type}, keys: {SUPPORTED_KEYS.get(item.type)}", + True, ) - ) - else: - log_debug( - f"[sensor|async_setup_entry|no support type found] {item.name}, type: {item.type}, keys: {SUPPORTED_KEYS.get(item.type)}", - True, - ) - - async_add_entities(entities) + + if len(entities): + async_add_entities(entities) class NexxtmoveSensor(NexxtmoveEntity, SensorEntity):