Skip to content

Commit

Permalink
v0.5.2 (#27)
Browse files Browse the repository at this point in the history
* Add config option for persistent notifications
* Set sensor.<name>_ev_time_to_full_charge to epoch when not charging
  • Loading branch information
G-Two authored Dec 19, 2021
1 parent e282f84 commit 52e3a7b
Show file tree
Hide file tree
Showing 12 changed files with 501 additions and 96 deletions.
463 changes: 398 additions & 65 deletions README.md

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion custom_components/subaru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from .const import (
CONF_COUNTRY,
CONF_NOTIFICATION_OPTION,
CONF_UPDATE_ENABLED,
COORDINATOR_NAME,
DOMAIN,
Expand Down Expand Up @@ -122,7 +123,11 @@ async def async_call_service(call):
if vin in vehicles:
if call.service != REMOTE_SERVICE_FETCH:
await async_call_remote_service(
hass, controller, call.service, vehicles[vin]
hass,
controller,
call.service,
vehicles[vin],
entry.options.get(CONF_NOTIFICATION_OPTION),
)
if call.service in SERVICES_THAT_NEED_FETCH:
await coordinator.async_refresh()
Expand Down
14 changes: 13 additions & 1 deletion custom_components/subaru/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
from homeassistant.core import callback
from homeassistant.helpers import aiohttp_client, config_validation as cv

from .const import CONF_COUNTRY, CONF_UPDATE_ENABLED, DOMAIN
from .const import (
CONF_COUNTRY,
CONF_NOTIFICATION_OPTION,
CONF_UPDATE_ENABLED,
DOMAIN,
NotificationOptions,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -151,6 +157,12 @@ async def async_step_init(self, user_input=None):
CONF_UPDATE_ENABLED,
default=self.config_entry.options.get(CONF_UPDATE_ENABLED, False),
): cv.boolean,
vol.Required(
CONF_NOTIFICATION_OPTION,
default=self.config_entry.options.get(
CONF_NOTIFICATION_OPTION, NotificationOptions.FAILURE.value
),
): vol.In(sorted(NotificationOptions.list())),
}
)
return self.async_show_form(step_id="init", data_schema=data_schema)
33 changes: 29 additions & 4 deletions custom_components/subaru/const.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
"""Constants for the Subaru integration."""
from enum import Enum

from homeassistant.const import Platform

DOMAIN = "subaru"
FETCH_INTERVAL = 300
UPDATE_INTERVAL = 7200
CONF_UPDATE_ENABLED = "update_enabled"
CONF_NOTIFICATION_OPTION = "notification_option"
CONF_COUNTRY = "country"


class NotificationOptions(Enum):
"""Lovelace levels of notification."""

FAILURE = "Failure — Only notify on failure"
PENDING = "Pending — Temporary notification of remote command in progress"
SUCCESS = "Success — Persistent notification of completed remote command"

@classmethod
def list(cls):
"""List values of NotificationOptions."""
return [item.value for item in NotificationOptions]

@classmethod
def get_by_value(cls, value):
"""Get enum instance by value."""
for item in cls:
if item.value == value:
return item


# entry fields
ENTRY_CONTROLLER = "controller"
ENTRY_COORDINATOR = "coordinator"
Expand Down Expand Up @@ -43,10 +68,10 @@
REMOTE_SERVICE_CHARGE_START = "charge_start"

SUPPORTED_PLATFORMS = [
"binary_sensor",
"device_tracker",
"lock",
"sensor",
Platform.BINARY_SENSOR,
Platform.DEVICE_TRACKER,
Platform.LOCK,
Platform.SENSOR,
]

ICONS = {
Expand Down
18 changes: 14 additions & 4 deletions custom_components/subaru/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from . import DOMAIN as SUBARU_DOMAIN
from .const import (
CONF_NOTIFICATION_OPTION,
ENTRY_CONTROLLER,
ENTRY_COORDINATOR,
ENTRY_VEHICLES,
Expand All @@ -25,7 +26,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
entities = []
for vehicle in vehicle_info.values():
if vehicle[VEHICLE_HAS_REMOTE_SERVICE]:
entities.append(SubaruLock(vehicle, coordinator, controller))
entities.append(SubaruLock(vehicle, coordinator, controller, config_entry))
async_add_entities(entities, True)


Expand All @@ -37,23 +38,32 @@ class SubaruLock(SubaruEntity, LockEntity):
Lock status is always unknown.
"""

def __init__(self, vehicle_info, coordinator, controller):
def __init__(self, vehicle_info, coordinator, controller, config_entry):
"""Initialize the locks for the vehicle."""
super().__init__(vehicle_info, coordinator)
self.entity_type = "Door Lock"
self.hass_type = LOCK_DOMAIN
self.controller = controller
self.config_entry = config_entry

async def async_lock(self, **kwargs):
"""Send the lock command."""
_LOGGER.debug("Locking doors for: %s", self.vin)
await async_call_remote_service(
self.hass, self.controller, SERVICE_LOCK, self.vehicle_info
self.hass,
self.controller,
SERVICE_LOCK,
self.vehicle_info,
self.config_entry.options.get(CONF_NOTIFICATION_OPTION),
)

async def async_unlock(self, **kwargs):
"""Send the unlock command."""
_LOGGER.debug("Unlocking doors for: %s", self.vin)
await async_call_remote_service(
self.hass, self.controller, SERVICE_UNLOCK, self.vehicle_info
self.hass,
self.controller,
SERVICE_UNLOCK,
self.vehicle_info,
self.config_entry.options.get(CONF_NOTIFICATION_OPTION),
)
4 changes: 2 additions & 2 deletions custom_components/subaru/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"config_flow": true,
"documentation": "https://github.com/G-Two/homeassistant-subaru",
"issue_tracker": "https://github.com/G-Two/homeassistant-subaru/issues",
"requirements": ["subarulink==0.3.15"],
"requirements": ["subarulink==0.3.16"],
"codeowners": ["@G-Two"],
"version": "0.5.0",
"version": "0.5.2",
"iot_class": "cloud_polling"
}
26 changes: 16 additions & 10 deletions custom_components/subaru/remote_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
VEHICLE_LAST_UPDATE,
VEHICLE_NAME,
VEHICLE_VIN,
NotificationOptions,
)

_LOGGER = logging.getLogger(__name__)
Expand All @@ -38,15 +39,17 @@
]


async def async_call_remote_service(hass, controller, cmd, vehicle_info):
async def async_call_remote_service(hass, controller, cmd, vehicle_info, notify_option):
"""Execute subarulink remote command with start/end notification."""
car_name = vehicle_info[VEHICLE_NAME]
vin = vehicle_info[VEHICLE_VIN]
hass.components.persistent_notification.create(
f"Sending {cmd} command to {car_name}\nThis may take 10-15 seconds",
"Subaru",
DOMAIN,
)
notify = NotificationOptions.get_by_value(notify_option)
if notify in [NotificationOptions.PENDING, NotificationOptions.SUCCESS]:
hass.components.persistent_notification.create(
f"Sending {cmd} command to {car_name}\nThis may take 10-15 seconds",
"Subaru",
DOMAIN,
)
_LOGGER.debug("Sending %s command command to %s", cmd, car_name)
success = False
err_msg = ""
Expand All @@ -60,11 +63,14 @@ async def async_call_remote_service(hass, controller, cmd, vehicle_info):
except SubaruException as err:
err_msg = err.message

hass.components.persistent_notification.dismiss(DOMAIN)
if notify in [NotificationOptions.PENDING, NotificationOptions.SUCCESS]:
hass.components.persistent_notification.dismiss(DOMAIN)

if success:
hass.components.persistent_notification.create(
f"{cmd} command successfully completed for {car_name}", "Subaru",
)
if notify == NotificationOptions.SUCCESS:
hass.components.persistent_notification.create(
f"{cmd} command successfully completed for {car_name}", "Subaru",
)
_LOGGER.debug("%s command successfully completed for %s", cmd, car_name)
return True

Expand Down
5 changes: 3 additions & 2 deletions custom_components/subaru/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
"step": {
"init": {
"title": "Subaru Starlink Options",
"description": "When enabled, vehicle polling will send a remote command to your vehicle every 2 hours to obtain new sensor data. Without vehicle polling, new sensor data is only received when the vehicle automatically pushes data (normally after engine shutdown).",
"description": "See documentation at: https://github.com/G-Two/homeassistant-subaru#options",
"data": {
"update_enabled": "Enable vehicle polling"
"update_enabled": "Enable vehicle polling (CAUTION: May drain battery after weeks of non-driving)",
"notification_option": "Lovelace UI notifications for remote commands"
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions custom_components/subaru/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
"step": {
"init": {
"title": "Subaru Starlink Options",
"description": "When enabled, vehicle polling will send a remote command to your vehicle every 2 hours to obtain new sensor data. Without vehicle polling, new sensor data is only received when the vehicle automatically pushes data (normally after engine shutdown).",
"description": "See documentation at: https://github.com/G-Two/homeassistant-subaru#options",
"data": {
"update_enabled": "Enable vehicle polling"
}
"update_enabled": "Enable vehicle polling (CAUTION: May drain battery after weeks of non-driving)",
"notification_option": "Lovelace UI notifications for remote commands"
}
}
}
}
}
2 changes: 1 addition & 1 deletion requirements.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ flake8
pytest
pytest-cov
pytest-homeassistant-custom-component
subarulink==0.3.15
subarulink==0.3.16
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from custom_components.subaru.const import (
CONF_COUNTRY,
CONF_NOTIFICATION_OPTION,
CONF_UPDATE_ENABLED,
DOMAIN,
FETCH_INTERVAL,
Expand All @@ -21,6 +22,7 @@
VEHICLE_HAS_REMOTE_START,
VEHICLE_HAS_SAFETY_SERVICE,
VEHICLE_NAME,
NotificationOptions,
)
from homeassistant.components.homeassistant import DOMAIN as HA_DOMAIN
from homeassistant.config_entries import ConfigEntryState
Expand Down Expand Up @@ -68,6 +70,7 @@

TEST_OPTIONS = {
CONF_UPDATE_ENABLED: True,
CONF_NOTIFICATION_OPTION: NotificationOptions.SUCCESS.value,
}

TEST_ENTITY_ID = "sensor.test_vehicle_2_odometer"
Expand Down
15 changes: 12 additions & 3 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
from subarulink.exceptions import InvalidCredentials, InvalidPIN, SubaruException

from custom_components.subaru import config_flow
from custom_components.subaru.const import CONF_UPDATE_ENABLED, DOMAIN
from custom_components.subaru.const import (
CONF_NOTIFICATION_OPTION,
CONF_UPDATE_ENABLED,
DOMAIN,
NotificationOptions,
)
from homeassistant import config_entries
from homeassistant.const import CONF_DEVICE_ID, CONF_PIN
from homeassistant.setup import async_setup_component
Expand All @@ -29,7 +34,6 @@


async def test_user_init_form(user_form):
"""Test the initial user form for first step of the config flow."""
"""Test the initial user form for first step of the config flow."""
assert user_form["description_placeholders"] is None
assert user_form["errors"] is None
Expand Down Expand Up @@ -173,10 +177,15 @@ async def test_pin_form_incorrect_pin(hass, pin_form):
async def test_option_flow(hass, options_form):
"""Test config flow options."""
result = await hass.config_entries.options.async_configure(
options_form["flow_id"], user_input={CONF_UPDATE_ENABLED: False},
options_form["flow_id"],
user_input={
CONF_NOTIFICATION_OPTION: NotificationOptions.PENDING.value,
CONF_UPDATE_ENABLED: False,
},
)
assert result["type"] == "create_entry"
assert result["data"] == {
CONF_NOTIFICATION_OPTION: NotificationOptions.PENDING.value,
CONF_UPDATE_ENABLED: False,
}

Expand Down

0 comments on commit 52e3a7b

Please sign in to comment.