Skip to content

Commit

Permalink
Update nest_protect
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaspence committed Oct 16, 2024
1 parent 866e680 commit 848d877
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 19 deletions.
6 changes: 5 additions & 1 deletion custom_components/nest_protect/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from . import HomeAssistantNestProtectData
from .const import CONF_COOKIES, CONF_ISSUE_TOKEN, CONF_REFRESH_TOKEN, DOMAIN
from .pynest.const import FULL_NEST_REQUEST

TO_REDACT = [
"access_token",
Expand All @@ -27,6 +28,7 @@
"location",
"longitude",
"name",
"parameters",
"pairing_token",
"postal_code",
"profile_image_url",
Expand Down Expand Up @@ -72,7 +74,9 @@ async def async_get_config_entry_diagnostics(

data = {
"app_launch": dataclasses.asdict(
await client.get_first_data(nest.access_token, nest.userid)
await client.get_first_data(
nest.access_token, nest.userid, request=FULL_NEST_REQUEST
)
)
}

Expand Down
2 changes: 1 addition & 1 deletion custom_components/nest_protect/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/imicknl/ha-nest-protect/issues",
"requirements": [],
"version": "0.4.0b5"
"version": "0.4.0b7"
}
16 changes: 12 additions & 4 deletions custom_components/nest_protect/pynest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ async def authenticate(self, access_token: str) -> NestResponse:
if nest_response.get("error"):
_LOGGER.error("Authentication error: %s", nest_response.get("error"))

raise PynestException(
f"{response.status} error while authenticating - {nest_response}."
)

try:
self.nest_session = NestResponse(**nest_response)
except Exception as exception:
Expand All @@ -232,12 +236,12 @@ async def authenticate(self, access_token: str) -> NestResponse:
return self.nest_session

async def get_first_data(
self, nest_access_token: str, user_id: str
self, nest_access_token: str, user_id: str, request: dict = NEST_REQUEST
) -> FirstDataAPIResponse:
"""Get first data."""
async with self.session.post(
APP_LAUNCH_URL_FORMAT.format(host=self.environment.host, user_id=user_id),
json=NEST_REQUEST,
json=request,
headers={
"Authorization": f"Basic {nest_access_token}",
"X-nl-user-id": user_id,
Expand All @@ -250,7 +254,11 @@ async def get_first_data(
result["_2fa_enabled"] = result.pop("2fa_enabled")

if result.get("error"):
_LOGGER.debug("Received error from Nest service", result)
_LOGGER.debug("Received error from Nest service", await response.text())

raise PynestException(
f"{response.status} error while subscribing - {result}"
)

result = FirstDataAPIResponse(**result)

Expand Down Expand Up @@ -294,7 +302,7 @@ async def subscribe_for_data(
"X-nl-protocol-version": str(1),
},
) as response:
_LOGGER.debug("Got data from Nest service (status: %s)", response.status)
_LOGGER.debug("Data received via subscriber (status: %s)", response.status)

if response.status == 401:
raise NotAuthenticatedException(await response.text())
Expand Down
29 changes: 29 additions & 0 deletions custom_components/nest_protect/pynest/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,32 @@
],
"known_bucket_versions": [],
}

FULL_NEST_REQUEST = {
"known_bucket_types": [
BucketType.BUCKETS,
BucketType.METADATA,
BucketType.KRYPTONITE,
BucketType.STRUCTURE,
BucketType.TOPAZ,
BucketType.WHERE,
BucketType.USER,
BucketType.DEMAND_RESPONSE,
BucketType.WIDGET_TRACK,
BucketType.OCCUPANCY,
BucketType.MESSAGE,
BucketType.MESSAGE_CENTER,
BucketType.LINK,
BucketType.SAFETY,
BucketType.SAFETY_SUMMARY,
BucketType.DEVICE_ALERT_DIALOG,
BucketType.QUARTZ,
BucketType.TOPAZ_RESOURCE,
BucketType.TRACK,
BucketType.TRIP,
BucketType.STRUCTURE_METADATA,
BucketType.USER,
BucketType.WIDGET_TRACK,
],
"known_bucket_versions": [],
}
4 changes: 4 additions & 0 deletions custom_components/nest_protect/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import PERCENTAGE, UnitOfTemperature
from homeassistant.helpers.entity import EntityCategory
Expand Down Expand Up @@ -68,6 +69,7 @@ class NestProtectSensorDescription(SensorEntityDescription):
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.DIAGNOSTIC,
bucket_type=BucketType.KRYPTONITE,
state_class=SensorStateClass.MEASUREMENT,
),
# TODO Due to duplicate keys, this sensor is not available yet
# NestProtectSensorDescription(
Expand All @@ -87,6 +89,7 @@ class NestProtectSensorDescription(SensorEntityDescription):
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.DIAGNOSTIC,
bucket_type=BucketType.TOPAZ,
state_class=SensorStateClass.MEASUREMENT,
),
NestProtectSensorDescription(
name="Replace By",
Expand All @@ -101,6 +104,7 @@ class NestProtectSensorDescription(SensorEntityDescription):
value_fn=lambda state: round(state, 2),
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
# TODO Add Color Status (gray, green, yellow, red)
# TODO Smoke Status (OK, Warning, Emergency)
Expand Down
3 changes: 2 additions & 1 deletion custom_components/nest_protect/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"config": {
"step": {
"user": {
"description": "Select your Account Type. Most users will need to select Google Account. Field Test is only available for selected testers in the Google Field Test program.",
"data": {
"account_type": "Account Type"
}
},
"account_link": {
"description": "Please retrieve your issue_token and cookies manually, following the instructions in the [integration README](https://github.com/iMicknl/ha-nest-protect/tree/beta#retrieving-issue_token-and-cookies) and paste them below.",
"description": "Unfortunately, Google does not provide an official API for this integration. To get it working, you will need to manually retrieve your issue_token and cookies by following the instructions in the integration README (https://github.com/iMicknl/ha-nest-protect/tree/beta#retrieving-issue_token-and-cookies). Please paste them below.",
"data": {
"issue_token": "[%key:common::config_flow::data::issue_token%]",
"cookies": "[%key:common::config_flow::data::cookies%]"
Expand Down
9 changes: 0 additions & 9 deletions custom_components/nest_protect/strings.select.json

This file was deleted.

7 changes: 4 additions & 3 deletions custom_components/nest_protect/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
},
"step": {
"user": {
"description": "Select your Account Type. Most users will need to select Google Account. Field Test is only available for selected testers in the Google Field Test program.",
"data": {
"account_type": "Account Type"
}
},
"account_link": {
"description": "Please retrieve your issue_token and cookies manually by following the instructions in the [integration README](https://github.com/iMicknl/ha-nest-protect/tree/beta#retrieving-issue_token-and-cookies) and paste them below.",
"description": "Unfortunately, Google does not provide an official API for this integration. To get it working, you will need to manually retrieve your issue_token and cookies by following the instructions in the integration README (https://github.com/iMicknl/ha-nest-protect/tree/beta#retrieving-issue_token-and-cookies). Please paste them below.",
"data": {
"issue_token": "issue_token",
"cookies": "cookies"
"issue_token": "Issue_token",
"cookies": "Cookies"
}
}
}
Expand Down

0 comments on commit 848d877

Please sign in to comment.