Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device PIN added as a sensor #15

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions custom_components/nexxtmove/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions custom_components/nexxtmove/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion custom_components/nexxtmove/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

_LOGGER = logging.getLogger(__name__)

PLATFORMS: Final = [Platform.SENSOR]
PLATFORMS: Final = [Platform.SENSOR, Platform.SWITCH]

ATTRIBUTION: Final = "Data provided by Nexxtmove"

Expand Down
2 changes: 1 addition & 1 deletion custom_components/nexxtmove/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions custom_components/nexxtmove/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class NexxtmoveItem:
name: str = ""
key: str = ""
type: str = ""
sensor_type: str = ""
state: str = ""
device_key: str = ""
device_name: str = ""
Expand Down
57 changes: 31 additions & 26 deletions custom_components/nexxtmove/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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):
Expand Down