# Dev module xiaomi v4

**URL:** <https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803>\
**Category:** Développement\
**Created:** [July 2, 2019, 6:36pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803 "2019-07-02T18:36:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 2, 2019, 6:36pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/1 "2019-07-02T18:36:29Z")

</div>

Hello everyone,

I’m currently developing the xiaomi module to integrate it into v4. For now, I want to have the minimum functionality (at this point I can retrieve the devices, including those for temperature and those for humidity).

I have a problem to modify my device on preact.

```javascript
const xiaomiCapteurTemperature = update(state.xiaomiCapteurTemperature, {
    [index]: {
        features: {
            [value.getAttribute('data-key')]: {
                [field]: {
                    $set: value.value
                }
            }
        }
    }
}

```

I want to modify the `device_feature` value of my device. For this, I retrieve `index` which is the device number (among the list of xiaomi devices), then `features` for the features attribute (which lists all the features such as temperatures, humidity, …) and then I have `data-key` which allows me to retrieve the feature number to get the right one. Then I want to modify `field` which is therefore the ‹&nbsp;name&nbsp;› column and then I finish by setting the value with the new value.

Why doesn’t it work? (front-end error)

```javascript
Uncaught ReferenceError: xiaomiCapteurTemperature is not defined

```

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![Boimb](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/boimb/32/225_2.png) [@Boimb](https://community.gladysassistant.com/u/Boimb)\
**Post date:** [July 2, 2019, 9:07pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/2 "2019-07-02T21:07:38Z")

</div>

Could you share a link to the sources? It’s a bit limited here 🙂

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 3, 2019, 5:20am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/3 "2019-07-03T05:20:14Z")

</div>

Yes, it’s not wrong, it’s a bit complicated to help without the sources 😃

> **[GitHub - damalgos/gladys-4-playground at xiaomi](https://github.com/damalgos/gladys-4-playground/tree/xiaomi)**
>
> REPO DISABLED, MOVED TO GLADYS MAIN REPO. Contribute to damalgos/gladys-4-playground development by creating an account on GitHub.

Be careful, I have a xiaomi branch, you must stay in it. And the files are in /front/src/routes/integrations/all/xiaomi

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 3, 2019, 6:44am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/4 "2019-07-03T06:44:28Z")

</div>

And I just realized I’m not on the official repo ^^

No problem, I’ll do everything properly later.

---

<div class="post-metadata">

**Author:** ![Boimb](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/boimb/32/225_2.png) [@Boimb](https://community.gladysassistant.com/u/Boimb)\
**Post date:** [July 3, 2019, 7:43am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/5 "2019-07-03T07:43:00Z")

</div>

When reading quickly, I would say you have a scope issue.  
Your variable is declared either in the if or in the else. Lines 75 and 87.  
By line 95, your variable is undefined as it is out of scope of your conditions.

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 3, 2019, 7:44am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/6 "2019-07-03T07:44:34Z")

</div>

Okay, I’ll check it out! 🙂 Thanks

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 3, 2019, 8:03am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/7 "2019-07-03T08:03:13Z")

</div>

> [@Boimb](#):
>
> ta var est undefined car hors scope de tes conditions.

Thanks!!! It works perfectly, what a beginner’s mistake 😃 Thanks a lot! It works perfectly

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [July 3, 2019, 8:52am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/8 "2019-07-03T08:52:22Z")

</div>

However, a small remark, don’t use `[value.getAttribute('data-key')]:`, that’s not good React practice at all ^^

I would advise you to create sub-components and manage this locally within the components, as I have done in the Z-Wave service for example, example here =\>

[https://github.com/GladysAssistant/Gladys/blob/master/front/src/routes/integration/all/zwave/node-page/Device.jsx](https://github.com/GladysAssistant/Gladys/blob/master/front/src/routes/integration/all/zwave/node-page/Device.jsx)

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 3, 2019, 9:12am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/9 "2019-07-03T09:12:21Z")

</div>

@pierre-gilles

I actually thought about that 😃 Thanks ^^ I’m getting used to React, I’m originally on Vue.js, it’s a bit different in some points 🙂

I’ll modify that.

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 3, 2019, 1:42pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/10 "2019-07-03T13:42:34Z")

</div>

@pierre-gilles

I just opened a PR, a question about the door detectors. How do you handle the fact that the detector can oscillate between 0 and 1? Isn’t that bothersome?

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [July 3, 2019, 1:58pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/11 "2019-07-03T13:58:00Z")

</div>

> [@damalgos](#):
>
> How do you handle the fact that the detector can oscillate between 0 and 1? Isn’t that bothersome?

Oscillate between 0 and 1? What do you mean?

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 3, 2019, 2:02pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/12 "2019-07-03T14:02:15Z")

</div>

> [@pierre-gilles](#):
>
> Oscillate between 0 and 1? What does that mean?

Well, normally if the two devices are next to each other they should be 1, but if they change state (at the limit of detectability, it could mess up, right?) Basically, it has trouble detecting them, so it randomly switches from 0 to 1 and potentially quickly.

I might be overcomplicating things ^^

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [July 3, 2019, 2:55pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/13 "2019-07-03T14:55:47Z")

</div>

You’re overcomplicating things!

I gave you a full review of your PR so you can improve its quality 🙂

PS: reply to me on the PR, not here

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 8, 2019, 9:01am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/15 "2019-07-08T09:01:25Z")

</div>

Hey @pierre-gilles,

I made some changes to the xiaomi service code, I’m waiting for your feedback on what to change or not 😊

Otherwise, I have a question, what is codecov exactly? I didn’t quite understand the principle, I saw that on my PR I have 60% but of what … ^^

---

<div class="post-metadata">

**Author:** ![Boimb](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/boimb/32/225_2.png) [@Boimb](https://community.gladysassistant.com/u/Boimb)\
**Post date:** [July 8, 2019, 10:13am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/16 "2019-07-08T10:13:25Z")

</div>

This is an indication of code coverage by tests.  
In short, you have 60% of your code covered by tests.

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 8, 2019, 10:16am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/17 "2019-07-08T10:16:16Z")

</div>

Oh thanks so I need to improve my tests ^^

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [July 9, 2019, 8:13am UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/18 "2019-07-09T08:13:11Z")

</div>

Indeed, the checks are failing because your coverage is low. You need to write tests for each function!

I’ll reply on the PR.

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 9, 2019, 12:26pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/19 "2019-07-09T12:26:33Z")

</div>

@pierre-gilles I wanted to know what you think about this. I have therefore merged the different possible Xiaomi sensors. However, when we take the example of the gateway or even the Xiaomi lamps, I think it is still important to separate these elements, don’t you think?

---

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [July 9, 2019, 12:52pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/20 "2019-07-09T12:52:56Z")

</div>

I agree, we can have multiple tabs, then we can group all the sensors together in my opinion.

However, these views are only the «&nbsp;what the service sees&nbsp;» views, but we need another view that groups all the devices «&nbsp;Gladys side&nbsp;» like on the Z-Wave service.

---

<div class="post-metadata">

**Author:** ![damalgos](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@damalgos](https://community.gladysassistant.com/u/damalgos)\
**Post date:** [July 9, 2019, 1:19pm UTC](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803/21 "2019-07-09T13:19:45Z")

</div>

OK, I’m making the changes and trying to set up the tests.

[Next page](https://community.gladysassistant.com/t/dev-module-xiaomi-v4/4803.md?page=2)
