Code Monkey home page Code Monkey logo

hubitatcontrol's Introduction

dd if=/dev/random of=/dev/sda # *Less > more, but there is no kill, like overkill...*
stateDiagram-v2
    [*] --> New_Idea
    New_Idea --> Working_On_Project:Write down idea
    Working_On_Project --> Bored:Life
    Bored --> New_Idea:Inspiration
    Project_Done --> [*] :Feeling of accomplishment
  • ๐Ÿ‘ฏ Iโ€™m looking to collaborate on anything Python related.
  • โšก Fun fact: A duck's quack doesn't echo, and no one knows why.

hubitatcontrol's People

Contributors

dependabot[bot] avatar jelloeater avatar

Stargazers

 avatar  avatar  avatar

hubitatcontrol's Issues

Need to implement HSL mapping

self.send_device_command(command="setColor", secondary_command=str(color))

sleep(2)

# TODO Need to implement HSL mapping

    def saturation(self) -> str:
        return [x for x in self.attributes if "saturation" in x["name"]][0]["currentValue"]

    def set_hue(self, hue: int):
        """0-100 valid range"""
        if hue < 0 or hue > 100:
            raise Exception("Invalid range")
        self.send_device_command(command="setHue", secondary_command=str(hue))
        sleep(2)

    def set_saturation(self, saturation: int):
        """0-100 valid range"""
        if saturation < 0 or saturation > 100:
            raise Exception("Invalid range")
        self.send_device_command(command="setSaturation", secondary_command=str(saturation))
        sleep(2)

    def set_color(self):
        raise NotImplementedError
        # TODO Need to implement HSL mapping
        # self.send_device_command(command="setColor", secondary_command=str(color))
        # sleep(2)

Refactor this to be dry

https://api.github.com/Jelloeater/hubitatcontrol/blob/f91f9531c80c7c7a4ea938ee6b510118f14690ee/hubitatcontrol/__init__.py#L56

    )  # Fall through return # pragma: no cover


# TODO Refactor this to be dry
def get_all_temperature_sensors(hub_in: hubitatcontrol.hub) -> list[hubitatcontrol.sensors.TemperatureSensor]:
    """Returns list of all hub devices with associated helper functions"""
    device_list = []
    for i in hub_in.devices:
        if "TemperatureMeasurement" in i["capabilities"]:
            device_list.append(get_device_type(i, hub_in))
    return device_list


def get_all_env_sensors(hub_in: hubitatcontrol.hub) -> list[hubitatcontrol.sensors.EnvironmentalSensor]:
    """Returns list of all hub devices with associated helper functions"""
    device_list = []
    for i in hub_in.devices:
        if ("RelativeHumidityMeasurement" in i["capabilities"]) and ("TemperatureMeasurement" in i["capabilities"]):
            device_list.append(get_device_type(i, hub_in))
    return device_list

-> Fix temp data get from EcoBee <-

https://api.github.com/Jelloeater/hubitatcontrol/blob/6cfe8757bff19d70f85d6342551e00d9181dab8d/hubitatcontrol/__init__.py#L52

"""Hubitat Maker API"""
import os

from dotenv import load_dotenv

import hubitatcontrol.generic as generic
import hubitatcontrol.lights as lights
import hubitatcontrol.sensors as sensors
from hubitatcontrol.hub import Hub


class GetSingleDevice:
    def __init__(self, hub_in: Hub):
        """
        Used to get a single device based on lookup
        """
        self.hub = hub_in

    def name(self, device_name: str):
        """
        Get a device by name and cast to the matched spec
        """
        for i in self.hub.devices:
            if i["name"] == device_name:
                return DeviceInit(device_in=i, hub_in=self.hub).cast_device()

    def id(self, device_id: int):
        """
        Get a device by id and cast to the matched spec
        """
        for i in self.hub.devices:
            if i["id"] == str(device_id):
                return DeviceInit(device_in=i, hub_in=self.hub).cast_device()


class GetDevices:
    def __init__(self, hub_in: Hub):
        """
        Get a list of pre-casted devices you can search though
        """
        self.hub = hub_in

    def __get_devices_from_capabilities__(self, capabilities_list: [str]):
        device_list = []
        for i in self.hub.devices:
            if all([x in i["capabilities"] for x in capabilities_list]):
                d = DeviceInit(self.hub, i).cast_device()
                device_list.append(d)
        return device_list

    def TemperatureSensor(self) -> list[sensors.TemperatureSensor]:
        # TODO -> Fix temp data get from EcoBee <-

        return self.__get_devices_from_capabilities__(sensors.TemperatureSensor.spec)

    def EnvironmentalSensor(self) -> list[sensors.EnvironmentalSensor]:
        return self.__get_devices_from_capabilities__(sensors.EnvironmentalSensor.spec)

    def Switch(self) -> list[generic.Switch]:
        return self.__get_devices_from_capabilities__(generic.Switch.spec)

    def Outlet(self) -> list[generic.ZigbeeOutlet]:
        return self.__get_devices_from_capabilities__(generic.ZigbeeOutlet.spec)

    def Dimmer(self) -> list[lights.Dimmer]:
        return self.__get_devices_from_capabilities__(lights.Dimmer.spec)

    def Bulb(self) -> list[lights.Bulb]:
        return self.__get_devices_from_capabilities__(lights.Bulb.spec)

    def ColorTempBulb(self) -> list[lights.ColorTempBulb]:
        return self.__get_devices_from_capabilities__(lights.ColorTempBulb.spec)

    def RGBWBulb(self) -> list[lights.RGBWBulb]:
        return self.__get_devices_from_capabilities__(lights.RGBWBulb.spec)


class DeviceInit:
    def __init__(self, hub_in: Hub, device_in: generic.Device):
        """
        This class is normally not used, as it's for dynamically casting devices
        """
        self.hub = hub_in
        self.device = device_in

    def cast_device(self):
        """The order here is very important that we cast the device properly based on increasing complexity /
        functionality"""
        c = self.device["capabilities"]

        if all([x in c for x in sensors.TemperatureSensor.spec]):
            return sensors.TemperatureSensor(self.hub, self.device)

        if all([x in c for x in sensors.EnvironmentalSensor.spec]):
            return sensors.EnvironmentalSensor(self.hub, self.device)

        if all([x in c for x in generic.ZigbeeOutlet.spec]):
            return generic.ZigbeeOutlet(self.hub, self.device)

        if all([x in c for x in lights.RGBWBulb.spec]):
            return lights.RGBWBulb(self.hub, self.device)

        if all([x in c for x in lights.ColorTempBulb.spec]):
            return lights.ColorTempBulb(self.hub, self.device)

        if all([x in c for x in lights.Dimmer.spec]):
            return lights.Dimmer(self.hub, self.device)

        if all([x in c for x in lights.Bulb.spec]):
            return lights.Bulb(self.hub, self.device)

        if all([x in c for x in lights.Switch.spec]):
            return lights.Switch(self.hub, self.device)


def get_hub_envs() -> Hub:
    """
    Generates a Hub object from local environmental variables
    """
    load_dotenv()
    host_env = os.getenv("HUBITAT_HOST")
    token_env = os.getenv("HUBITAT_API_TOKEN")
    app_id_env = os.getenv("HUBITAT_API_APP_ID")
    cloud_token = os.getenv("HUBITAT_CLOUD_TOKEN")
    return Hub(host=host_env, token=token_env, app_id=app_id_env, cloud_token=cloud_token)

Add button properties

# TODO Add button properties

    def power(self) -> int:
        """Returns power usage"""
        return [x for x in self.attributes if "power" in x["name"]][0]["currentValue"]


class Thermostat(Device):
    def set_temperature(self):
        raise NotImplementedError


class EcoBee(Thermostat):
    # TODO Need to implement generic and EcoBee specific functions
    pass


class GenericZWaveLock(Device):
    # TODO Add ZWave lock
    def lock(self):
        raise NotImplementedError

    def unlock(self):
        raise NotImplementedError


class Button(Device):
    pass


class SonoffZigbeeButtonController(Button):
    # TODO Add button properties
    pass

Need to implement generic and EcoBee specific functions

# TODO Need to implement generic and EcoBee specific functions

    def power(self) -> int:
        """Returns power usage"""
        return [x for x in self.attributes if "power" in x["name"]][0]["currentValue"]


class Thermostat(Device):
    def set_temperature(self):
        raise NotImplementedError


class EcoBee(Thermostat):
    # TODO Need to implement generic and EcoBee specific functions
    pass


class GenericZWaveLock(Device):
    # TODO Add ZWave lock
    def lock(self):
        raise NotImplementedError

    def unlock(self):
        raise NotImplementedError


class Button(Device):
    pass


class SonoffZigbeeButtonController(Button):
    # TODO Add button properties
    pass

Add Sensor gets

def TemperatureSensor(self) -> list[hubitatcontrol.sensors.TemperatureSensor]:

return self.get_devices_from_capabilities(["TemperatureMeasurement"])

def EnvironmentalSensor(self) -> list[hubitatcontrol.sensors.EnvironmentalSensor]:

return self.get_devices_from_capabilities(["RelativeHumidityMeasurement", "TemperatureMeasurement"])

def Switch(self) -> list[hubitatcontrol.generic.Switch]:

return self.get_devices_from_capabilities(["PowerMeter", "Outlet"])

def Dimmer(self) -> list[hubitatcontrol.lights.Dimmer]:

return self.get_devices_from_capabilities(["SwitchLevel"])

def Bulb(self) -> list[hubitatcontrol.lights.Bulb]:

return self.get_devices_from_capabilities(["ChangeLevel"])

def ColorTempBulb(self) -> list[hubitatcontrol.lights.ColorTempBulb]:

return self.get_devices_from_capabilities(["ColorTemperature"])

def RGBWBulb(self) -> list[hubitatcontrol.lights.RGBWBulb]:

return self.get_devices_from_capabilities(["ColorControl", "ColorMode"])

return hubitatcontrol.sensors.TemperatureSensor(self.hub, self.device)

if ["RelativeHumidityMeasurement", "TemperatureMeasurement"] in c:

return hubitatcontrol.sensors.TemperatureSensor(self.hub, self.device)

return self.get_devices_from_capabilities(["RelativeHumidityMeasurement", "TemperatureMeasurement"])

def Outlet(self) -> list[hubitatcontrol.generic.ZigbeeOutlet]:

return self.get_devices_from_capabilities(["PowerMeter", "Outlet"])

def Dimmer(self) -> list[hubitatcontrol.lights.Dimmer]:

return self.get_devices_from_capabilities(["SwitchLevel"])

def Bulb(self) -> list[hubitatcontrol.lights.Bulb]:

return self.get_devices_from_capabilities(["ChangeLevel"])

def ColorTempBulb(self) -> list[hubitatcontrol.lights.ColorTempBulb]:

return self.get_devices_from_capabilities(["ColorTemperature"])

def RGBWBulb(self) -> list[hubitatcontrol.lights.RGBWBulb]:

return self.get_devices_from_capabilities(["ColorControl", "ColorMode"])

https://api.github.com/Jelloeater/hubitatcontrol/blob/69d8f06b0d8ce3d3c0ca005042dbfcc21b665cd6/hubitatcontrol/__init__.py#L21

        device_list = []
        for i in self.hub.devices:
            if all([x in i["capabilities"] for x in capabilities_list]):
                d = GetDevice(self.hub, i).cast_device()
                device_list.append(d)
        return device_list

    # TODO Add Sensor gets
    # def TemperatureSensor(self) -> list[hubitatcontrol.sensors.TemperatureSensor]:
    #     return self.__get_devices_from_capabilities__(["TemperatureMeasurement"])
    #
    # def EnvironmentalSensor(self) -> list[hubitatcontrol.sensors.EnvironmentalSensor]:
    #     return self.__get_devices_from_capabilities__(["RelativeHumidityMeasurement", "TemperatureMeasurement"])
    #
    def Switch(self) -> list[hubitatcontrol.generic.Switch]:
        return self.__get_devices_from_capabilities__(["Switch"])

    #
    # def Outlet(self) -> list[hubitatcontrol.generic.ZigbeeOutlet]:
    #     return self.__get_devices_from_capabilities__(["PowerMeter", "Outlet"])
    #
    # def Dimmer(self) -> list[hubitatcontrol.lights.Dimmer]:
    #     return self.__get_devices_from_capabilities__(["SwitchLevel"])
    #
    # def Bulb(self) -> list[hubitatcontrol.lights.Bulb]:
    #     return self.__get_devices_from_capabilities__(["ChangeLevel"])
    #
    # def ColorTempBulb(self) -> list[hubitatcontrol.lights.ColorTempBulb]:
    #     return self.__get_devices_from_capabilities__(["ColorTemperature"])
    #
    # def RGBWBulb(self) -> list[hubitatcontrol.lights.RGBWBulb]:
    #     return self.__get_devices_from_capabilities__(["ColorControl", "ColorMode"])


class GetDevice:
    def __init__(self, hub_in: hubitatcontrol.hub, device_in: hubitatcontrol.generic.Device):
        self.hub = hub_in
        self.device = device_in

    def cast_device(self):
        if all([x in self.device["capabilities"] for x in ["Switch"]]):
            return hubitatcontrol.lights.Switch(self.hub, self.device)

        # if ["TemperatureMeasurement"] in c:
        #     return hubitatcontrol.sensors.TemperatureSensor(self.hub, self.device)
        #
        # if ["RelativeHumidityMeasurement", "TemperatureMeasurement"] in c:
        #     return hubitatcontrol.sensors.TemperatureSensor(self.hub, self.device)

        # def EnvironmentalSensor(self) -> list[hubitatcontrol.sensors.EnvironmentalSensor]:
        #     return self.__get_devices_from_capabilities__(["RelativeHumidityMeasurement", "TemperatureMeasurement"])
        #
        #
        # def Outlet(self) -> list[hubitatcontrol.generic.ZigbeeOutlet]:
        #     return self.__get_devices_from_capabilities__(["PowerMeter", "Outlet"])
        #
        # def Dimmer(self) -> list[hubitatcontrol.lights.Dimmer]:
        #     return self.__get_devices_from_capabilities__(["SwitchLevel"])
        #
        # def Bulb(self) -> list[hubitatcontrol.lights.Bulb]:
        #     return self.__get_devices_from_capabilities__(["ChangeLevel"])
        #
        # def ColorTempBulb(self) -> list[hubitatcontrol.lights.ColorTempBulb]:
        #     return self.__get_devices_from_capabilities__(["ColorTemperature"])
        #
        # def RGBWBulb(self) -> list[hubitatcontrol.lights.RGBWBulb]:
        #     return self.__get_devices_from_capabilities__(["ColorControl", "ColorMode"])

Add ZWave lock

# TODO Add ZWave lock

    def power(self) -> int:
        """Returns power usage"""
        return [x for x in self.attributes if "power" in x["name"]][0]["currentValue"]


class Thermostat(Device):
    def set_temperature(self):
        raise NotImplementedError


class EcoBee(Thermostat):
    # TODO Need to implement generic and EcoBee specific functions
    pass


class GenericZWaveLock(Device):
    # TODO Add ZWave lock
    def lock(self):
        raise NotImplementedError

    def unlock(self):
        raise NotImplementedError


class Button(Device):
    pass


class SonoffZigbeeButtonController(Button):
    # TODO Add button properties
    pass

Finish name method for README

https://api.github.com/Jelloeater/hubitatcontrol/blob/68455779b9c8d150bfa4d65ddc761303218e6251/hubitatcontrol/__init__.py#L15

"""Hubitat Maker API"""
import os

from dotenv import load_dotenv

import hubitatcontrol.generic as generic
import hubitatcontrol.lights as lights
import hubitatcontrol.sensors as sensors
from hubitatcontrol.hub import Hub


class GetSingleDevice:
    @staticmethod
    def name(device_name: str):
        # TODO Finish name method for README
        h = get_hub_envs()
        for i in h.devices:
            if i["name"] == device_name:
                return i


class GetDevices:
    def __init__(self, hub_in: Hub):
        self.hub = hub_in

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.