Giter Site home page Giter Site logo

pyrituals's Introduction

PyRituals package

PyPI PyPI - Downloads PyPI - License

An async Python wrapper for the Rituals Perfume Genie API. It allows you to control the diffuser and retrieve its state. The package supports the first and second version.

Installation

pip install pyrituals

Usage

Import

from pyrituals import Account, Diffuser, AuthenticationException

Create a aiohttp.ClientSession to make requests

from aiohttp import ClientSession
session = ClientSession()

Endpoints

Default endpoints:

AUTH_URL = "https://rituals.sense-company.com/ocapi/login"           # Account.authenticate()
ACCOUNT_URL = "https://rituals.sense-company.com/api/account/hubs"   # Account.get_devices()
HUB_URL = "https://rituals.sense-company.com/api/account/hub"        # Diffuser.update_data()
UPDATE_URL = "https://rituals.sense-company.com/api/hub/update/attr" # Diffuser.turn_*(), Diffuser.set_*()

To change the used API endpoints add an url parameter to the function. Example:

LOGIN_URL = "https://rituals.sense-company.com/ocapi/login"
account = Account("[email protected]", "passw0rd", session)
await account.authenticate(url=LOGIN_URL)

Account

Create an instance

email = "[email protected]"
password = "passw0rd"

account = Account(email, password, session)

Authenticate

try:
    await account.authenticate()
except AuthenticationException as e:
    print("Could not authenticate:", e)

Account data

The account data is only available after authentication.

account.data

Get linked devices

get_devices() returns a list of Diffusers.

devices = await account.get_devices()

Diffuser

Diffuser data

The initial data and format is different from the data after executing update_data(). Some properties require data that is only available after executing update_data(). Therefore, it's required to execute update_data() before using the diffuser properties.

diffuser.data
diffuser.battery_percentage
diffuser.charging
diffuser.has_battery
diffuser.has_cartridge
diffuser.hash
diffuser.hub_data
diffuser.hublot
diffuser.fill
diffuser.perfume
diffuser.perfume_amount
diffuser.room_size
diffuser.is_on
diffuser.is_online
diffuser.name
diffuser.version
diffuser.wifi_percentage
diffuser.room_size_square_meter

Get updated data

await diffuser.update_data()

Turn the diffuser on

await diffuser.turn_on()

Turn the diffuser off

await diffuser.turn_off()

Set the diffuser perfume amount

Amount must be an integer between 1 and 3, inclusive.

amount = 1
await diffuser.set_perfume_amount(amount)

Set the diffuser room size

Size must be an integer between 1 and 4, inclusive.

size = 2
await diffuser.set_room_size(size)

Set the diffuser room size in square meters

Size must be the integer 15, 30, 60 or 100.

size = 60
await diffuser.set_room_size_square_meter(size)

Example

from aiohttp import ClientSession
from asyncio import run

import pyrituals

async def main():
    async with ClientSession() as session:
        account = pyrituals.Account("[email protected]", "passw0rd", session)
        try:
            await account.authenticate()
        except pyrituals.AuthenticationException as ex:
            print("Could not authenticate:", ex)
            return
        print("Account data:", account.data)
        devices = await account.get_devices()
        for diffuser in devices:
            print("Diffuser data:", diffuser.data)
            await diffuser.turn_on()
            await diffuser.set_perfume_amount(1)
            await diffuser.set_room_size(4)
            await diffuser.update_data()
            print("Diffuser updated data:", diffuser.data)
            if diffuser.has_battery:
                print(f"Battery percentage: {diffuser.battery_percentage}%")

run(main())

pyrituals's People

Contributors

milanmeu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

pyrituals's Issues

KeyError: 'fanc' when using diffuser.is_on

Milan,

can you check this,

diffuser.is_on is giving error :

traceback (most recent call last):
File "/home/pi/scripts/genie.py", line 33, in
run(main())
File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/home/pi/scripts/genie.py", line 28, in main
print(f'stat: : {diffuser.is_on}')
File "/home/pi/.local/lib/python3.9/site-packages/pyrituals/init.py", line 71, in is_on
return self.hub_data["attributes"]["fanc"] == "1"
KeyError: 'fanc'

when i use the example, and add the 'diffuser.is_on'

(diffuser.perfume_amount is also not working)

i am using python3.9 on rpi4 bullseye

regards,
Chris

Error when reading attribute speedc

In HomeAssistant integration, which uses this library, I get the following error:

ValueError: invalid literal for int() with base 10: ''
2021-07-16 08:29:23 ERROR (MainThread) [homeassistant.components.switch] Error while setting up rituals_perfume_genie platform for switch
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 258, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 383, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 588, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 666, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 419, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 456, in _async_write_ha_state
    extra_state_attributes = self.extra_state_attributes
  File "/usr/src/homeassistant/homeassistant/components/rituals_perfume_genie/switch.py", line 50, in extra_state_attributes
    "fan_speed": self._diffuser.perfume_amount,
  File "/usr/local/lib/python3.9/site-packages/pyrituals/__init__.py", line 63, in perfume_amount
    return int(self.hub_data["attributes"]["speedc"])

Versions:
Home Assistant: 2021.7.2
pyrituals: 0.0.4

My guess: Either the speedc attribute is incorrect or it is not filled in all cases.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.